2

This is the xml string. I am trying to get the item inside the list Item can be 1 or more. I want to get item values like Id, pay... But I cannot reach to the items. How can I get access to this values with dom4j?

<?xml version="1.0" encoding="UTF-8"?>
<data>
 <cm>Detail</cm>
 <Code>1</Code>
 <No>20170928</No>
 <Num>1</Num>
 <Flag>Y</end_Flag>
 <list>
  <items>
   <item>
    <Id>01234567</Id>
    <pay>5555</pay>
    <remarksInfo></remarksInfo>
    <d_Date>2017-09-28 16:26:55</d_Date>
    <amount>1.0</amount>
    <Name>ADAM</Name>
    <e_Date>2017-09-28 16:26:55</e_Date>
    <t_Date>2017-09-28 17:46:39</t_Date>
    <damount>1.0</damount>
    <fee>3.0</fee>
    <e_Name>Smith</e_Name>
    <abstractInfo></abstractInfo>
    <Status>S</Status>
    <Code>0026</Code>
    <l_Desc></l_Desc>
    <note></note>
   </item>
<item>
    <Id>01234567</Id>
    <pay>5555</pay>
    <remarksInfo></remarksInfo>
    <d_Date>2017-09-28 16:26:55</d_Date>
    <amount>1.0</amount>
    <Name>ADAM</Name>
    <e_Date>2017-09-28 16:26:55</e_Date>
    <t_Date>2017-09-28 17:46:39</t_Date>
    <damount>1.0</damount>
    <fee>3.0</fee>
    <e_Name>Smith</e_Name>
    <abstractInfo></abstractInfo>
    <Status>S</Status>
    <Code>0026</Code>
    <l_Desc></l_Desc>
    <note></note>
   </item>
  </items>
 </list>
 <hmac>123</hmac>
</data>

I really appreciate your help!

1
  • What are you trying and how does it fail? Commented Sep 28, 2017 at 17:03

2 Answers 2

7

I was struggling with the same problem and discovered the solution in the DOM4J Documentation.

If you have some XML as a String you can parse it back into a Document again using the helper method DocumentHelper.parseText()

String text = " James";

Document document = DocumentHelper.parseText(text);

Sign up to request clarification or add additional context in comments.

Comments

-1
SAXReader reader = new SAXReader();
         Document document = reader.read( inputFile );

         Element classElement = document.getRootElement();

         List<Node> nodes = document.selectNodes("/data/list/items" );
         System.out.println("----------------------------");

         for (Node node : nodes) {
        //get ID & Pay under item
           String id = node.selectSingleNode("Id").getText()
           String pay = node.selectSingleNode("Pay").getText()

         }

In for loop, extract & under .

3 Comments

But xml is String not file
There are multiple variants of read() method. Check dom4j.github.io/javadoc/2.0.1/org/dom4j/io/…
Specifically, there is a read() method that accepts a Reader, so you can give it a StringReader() that wraps your XML string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.