Java-to-XML attribute parsing capabilities

Source: Internet
Author: User
Tags xml attribute

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-to-XML attribute parsing capabilities

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.