码迷,mamicode.com
首页 > 编程语言 > 详细

Java对XML属性解析功能

时间:2018-02-11 12:31:42      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:roo   stat   ber   sbo   enc   sms   解析   type   oid   

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属性解析功能

标签:roo   stat   ber   sbo   enc   sms   解析   type   oid   

原文地址:http://blog.51cto.com/11964104/2071048

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!