Java对XML属性解析功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java对XML属性解析功能相关的知识,希望对你有一定的参考价值。
package usi.ipms.test;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class ParseCasXmlString {
public static void main(String[] args) {
String XmlString="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<AuthenChallengeMsg>"+
"<MessageType type=\"7\">"+
"</MessageType>"+
"<Protocol version=\"2.1\"></Protocol>"+
"<ID name=\"\"></ID>"+
"<params></params>"+
"<SessionID sessionid=\"bd08417d-5a1a-4ca1\"></SessionID>"+
"<Challenges>"+
"<DynamicPwdChallenge authenNumber=\"1\" minPwdSize=\"0\" authenMode=\"smscha\" allowBeginTime=\"\" allowEndTime=\"\" type=\"casbox\" isAllowTime=\"AllowTime\" allowMaxTime=\"6\" applyReason=\"\" challenge=\"d2FuZ3RhbyznjvmtpssMTczOTgzOTI4OTIsMEBAbWFsb25nLOmprOm+mSwxNzM5ODM5Mjg5MiwwQEB6aGuZ2h5OW8oOa1t+awuCwxNzc1NjA5MzAzNSww\">"+
"</DynamicPwdChallenge>"+
"</Challenges>"+
"</AuthenChallengeMsg>";
try {
Document document = DocumentHelper.parseText(XmlString);
//获取根节点元素,此处为<AuthenChallengeMsg>
Element node = document.getRootElement();
//通过element()获得指定节点名称的节点,在这里是<SessionID>
Element SessionID = node.element("SessionID");
//获得所在节点的节点名称
String elementName = SessionID.getName();
//获取<西游记>节点所具有的属性,输入需要的属性名称id
Attribute attribute = SessionID.attribute("sessionid");
//获得属性文本 x001
String txt = attribute.getText();
System.out.println("SessionID:"+SessionID);
System.out.println("elementName:"+elementName);
System.out.println("attribute:"+attribute);
System.out.println("txt:"+txt);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上是关于Java对XML属性解析功能的主要内容,如果未能解决你的问题,请参考以下文章