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