Java XML Node属性不返回空白字符

时间:2014-03-05 20:07:54

标签: java xml

我有一个XML文档(从数据库中读取),当我将其加载到文档时,它有空白/换行符,节点属性缺少空格/换行符。

有没有办法保留空白/换行信息?

样品

<Message>
  <Header format="qwe" standard="123.456">
   <Version value="2"/>
     <G1>
       <Originator value="google"/>
    </G1>
  </Header>
 <Body format="xyz" standard="abc" type="123">
   <Comments value="uuuuuuuuuuuuuuuuuuu
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj



                     ttttttttttttttttt
                      44444
                       3333
                        22
   "/>

   </Body>
</Message>

代码:

 String parseXML = new String(this.messageXML);
 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
 Document documentMessage = builder.parse(new InputSource(new StringReader(parseXML)));
 NodeList bodyNodes = documentMessage.getElementsByTagName("Comments");
 if (bodyNodes.getLength() > 0) {
    System.out.println("Nodelist has something.." + bodyNodes.getLength());
    for(int nodeCount = 0; nodeCount < bodyNodes.getLength(); nodeCount++) {
       Node localNode = bodyNodes.item(nodeCount);
        System.out.println("localNodeValue... " + localNode.getAttributes().getNamedItem("value").getTextContent());

        }
    }

0 个答案:

没有答案