Java對XML屬性解析功能

來源:互聯網
上載者:User

標籤: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屬性解析功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.