当前位置: 首页 > 工具软件 > kXML > 使用案例 >

利用kxml2解析和构造xml

宣煜
2023-12-01

在线文档路径

构造xml

xml的内容

<head xmlns="http://testxmlns">
    <UsernameToken>
        <Username>user123</Username>
        <Password Type="http://testtype">
            pass123
        </Password>
    </UsernameToken>
</head>

利用对象Element构造xml

将xml解析为树状结构来构造

Element[] header = new Element[1];
//创建新的元素节点,参数1:命名空间,参数2:节点命
header[0] = new Element().createElement(null, "head");
//为节点设置属性,参数1:命名空间,参数2:属性名,参数3:属性值
header[0].setAttribute(null,"xmlns", "http://testxmlns");
Element UsernameToken = new Element().createElement(null, "UsernameToken"); 
Element userName = new Element().createElement(null, "Username");
//添加子节点,参数1:节点类型,参数2:节点对象
//Node.TEXT:文本节点,Node.ELEMENT:元素节点
userName.addChild(Node.TEXT, "user123");
Element passWord = new Element().createElement(null, "Password"); 
passWord.addChild(Node.TEXT, "pass123");
passWord.setAttribute(null,"Type", "http://testtype");
UsernameToken.addChild(Node.ELEMENT, userName);
UsernameToken.addChild(Node.ELEMENT, passWord);
header[0].addChild(Node.ELEMENT, UsernameToken);
//

利用KXmlSerializer构造xml的字符串

KXmlSerializer序列化按xml的顺序构造xml

//实例化KXmlSerializer
KXmlSerializer serializer = new KXmlSerializer();
//KXmlSerializer继承类XmlSerializer,所以也可以实例化XmlSerializer来构造xml
//XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
//XmlSerializer serializer = factory.newSerializer();
StringWriter writer = new StringWriter();
//必须要有输出
serializer.setOutput(writer);
//设置输出自动按xml格式缩进 ,false不缩进
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
//生成声明行<?xml version="1.0" encoding="utf-8"?>
//serializer.startDocument("utf-8", null);
//设置节点名
serializer.startTag("", "head");
//设置节点属性
serializer.attribute("", "xmlns", "http://testxmlns");
serializer.startTag("", "UsernameToken");
serializer.startTag("", "Username");
//设置节点值
serializer.text("user123");
serializer.endTag("","Username");
serializer.startTag("", "Password");
serializer.attribute("", "Type","http://testtype");
serializer.text("pass123");
serializer.endTag("","Password");
serializer.endTag("","UsernameToken");
serializer.endTag("","Security");
//对应startDocument
//serializer.endDocument();
System.out.println(writer.toString());

将Element格式的xml转化为String

KXmlSerializer serializer = new KXmlSerializer();
StringWriter writer = new StringWriter();
serializer.setOutput(writer);
//将xml数据写入对象
header[0].write(serializer);
//设置自动缩进
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
System.out.println(writer.toString());

从文件或者网络中读取xml并解析

从网络获取xml并存入文件中

//获取网络输入流
InputStream in=new URL("http://www.baidu.com").openConnection().getInputStream();
//下面从文件获取输入流
//File f1= new File("1.xml");
//FileInputStream in = new FileInputStream(f1);
File f2= new File("2.xml");
//定义输出位置
FileOutputStream fo = new FileOutputStream(f2);
//每次读取的文件大小
byte[] bytes = new byte[1024];
//定义文件指针
int byteread;
while ((byteread = in.read(bytes)) != -1) {    
    fo.write(bytes, 0, byteread);  
}  
in.close();
fo.close();

解析字节流xml

注:parse.getAttributeName必须在parse.nextText()之前

InputStream in=new URL("http://www.baidu.com").openConnection().getInputStream();
KXmlParser parse=new KXmlParser();
parse.setInput(in,"utf-8");
boolean keepParsing=true;
while(keepParsing){
    int type = parse.next();
    switch (type) {
    case KXmlParser.START_DOCUMENT:  
        break;     
    case KXmlParser.START_TAG:     
        if("Username".endsWith(parse.getName())){
            System.out.println("userNamestr======="+parse.nextText());
        }else if("Password".endsWith(parse.getName())){
        //parse.getAttributeName必须在parse.nextText()之前
        //参数为索引几个属性
            System.out.println("passWordstr_type======="+parse.getAttributeName(0)+"====="+parse.getAttributeValue(0));
            System.out.println("passWordstr======"+parse.nextText().trim());
        }
        break;     
    case KXmlParser.END_TAG:     
        break;     
    case KXmlParser.TEXT:     
        break;     
    case KXmlParser.END_DOCUMENT:
    //判断是否结束  
        keepParsing = false;  
        break;     
    }     
}
 类似资料: