1

I have a XML document (read from a db) that has whitespace/newline characters when I load it in to a Document the Node Attribute is missing the whitespace/newline characters..

Is there a way to keep the whitespace/newline information?

Sample

<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>

code:

 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());

        }
    }
user2106070
  • 151
  • 1
  • 1
  • 7
  • You should look into <![CDATA[]]> blocks -- [tutorial here](http://www.w3schools.com/xml/xml_cdata.asp) – ProgrammerDan Mar 05 '14 at 20:19
  • 1
    Looks like this is just attribute value normalization: http://www.w3.org/TR/REC-xml/#AVNormalize - not sure whether it *can* be disabled... – Jon Skeet Mar 05 '14 at 20:20
  • 1
    possible duplicate of: [http://stackoverflow.com/questions/2004386/how-to-save-newlines-in-xml-attribute](http://stackoverflow.com/questions/2004386/how-to-save-newlines-in-xml-attribute) – Max Meijer Mar 05 '14 at 20:23
  • Thanks - I cannot change the XML so I'm stuck with the format, I was hoping there was a way around the attribute value normalization. – user2106070 Mar 05 '14 at 21:25

0 Answers0