dom4j parses xml to get node value and node attribute

Document document = null;

//xmlString is the data in xml format of String type

String xmlString="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+"<root>"
+"<data ret_code=\"0\" error_message=\"失败原因\">"
+"<refund_no>That's it</refund_no>"
+"</data>"
+"<sign>xxxxxxxxxxxxxxxxxxx</sign>"
+"</root>";


try {
document = DocumentHelper.parseText(xmlString);
System.out.println("document"+document);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace ();

}  

//Get the root node element object  
        Element root = document.getRootElement();  

// get child nodes

 System.out.println("Name of current node: " + root .getName());  
        
        //
        Element data = root .element("data");
//Get child node

        String ret_code= data.attributeValue("ret_code");//Get the value of the child node attribute
        String error_message= data.attributeValue("error_message");//Get the value of the child node attribute
        System.out.println("ret_code:" +ret_code+"error_message:"+error_message);
        String text= data.elementText("refund_no");//Get the value of the node
        Stringsign= node.elementText("sign");Get the text of the node
        System.out.println(text+sign+">>>");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325816028&siteId=291194637