简体   繁体   中英

XML DOM parsing with Java

I'm trying to parse this XML string:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<response type="success">
   <lots>
      <lot>32342</lot>
      <lot>52644</lot>
   </lots>
 </response>

When I get the root node, which is "response", I use the method getChildNodes() which returns a NodeList of length 3. However what I'm confused about is the order the NodeList gets created in. I used some print statements to show whats in the list

Item length: 3
Item (0): [#text: 
]
Item (1): [lots: null]
Item (2): [#text: 
]

So the text node is first which is two levels below the root, then the next child of the root, and then the next text node.

Is there a particular order and/or reason the list is ordered in this way?

You are seeing the whitespace text surrounding the childnode "lots"

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<response type="success">
{txt1}<lots>
         <lot>32342</lot>
         <lot>52644</lot>
      </lots>{txt2}
 </response>

实际上我发现了问题,我使用的是'\\ n'字符,这导致解析了额外的文本节点

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM