1

i'm using org.apache.xerces.jaxp.DocumentBuilderImpl for loading xml documents in java. Document to load is:

<?xml version="1.0" encoding="UTF-8"?>CRLF
<doc >CRLF
  <e1   />CRLF
</doc>

I load document in common way:

 DocumentBuilder builderXml = null;
 Document nodeXml = null;
 ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlByte);
 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
 documentBuilderFactory.setNamespaceAware(true);
 builderXml = documentBuilderFactory.newDocumentBuilder();
 nodeXml = builderXml.parse(inputStream);

Loaded document seems to be ok, but only one thing is missing. CR at the end of line was ommited.

If I call this

nodeXml.getChildNodes().item(0).getChildNodes().item(0).getNodeValue()

I get "\n " string.

In normal situation isn't this problem, but in combined with canonicalization I get different result as I expected. Can me someone help what is wrong with CR at the end line ?

Java SDK 1.7_25 x86

Thank in advance for your help

Vlado

EDIT:

In .net I can write this

var xDoc = new XmlDocument();
xDoc.PreserveWhitespace = true;
using (var fs = new FileStream("file.xml", FileMode.Open))
{
    xDoc.Load(fs);
}

var transform = new XmlDsigC14NTransform(false) { Algorithm = SignedXml.XmlDsigC14NTransformUrl };

transform.LoadInput(xDoc);
var output = (MemoryStream)transform.GetOutput();

File.WriteAllBytes("C:\\file1.xml", output.ToArray());

and whitespaces are preserved. Is this possible in java ?

eldrex
  • 143
  • 9

1 Answers1

0

The XML standard states:

XML parsed entities are often stored in computer files which, for editing convenience, are organized into lines. These lines are typically separated by some combination of the characters CARRIAGE RETURN (#xD) and LINE FEED (#xA).

To simplify the tasks of applications, the XML processor MUST behave as if it normalized all line breaks in external parsed entities (including the document entity) on input, before parsing, by translating both the two-character sequence #xD #xA and any #xD that is not followed by #xA to a single #xA character.

So what you see is in fact expected behaviour.

Community
  • 1
  • 1
Henry
  • 42,982
  • 7
  • 68
  • 84