0

My code

InputStream ins = getResources().openRawResource(
        getResources().getIdentifier("state",
                "raw", getPackageName()));
DocumentBuilder docBuilder = null;
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setIgnoringElementContentWhitespace(true);
docBuilder = f.newDocumentBuilder();

state = docBuilder.parse(ins);
ins.close();

The xml to understand what happens

<?xml version="1.0" encoding="utf-8"?>
<State xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <IsOff>false</IsOff>
</State>

And the result you can see as \n; IsOff and \n

enter image description here

Also, you can see that I've set

f.setIgnoringElementContentWhitespace(true);

This XML would come from anywhere, so it is unacceptable to have such parsing results. Also, whiteSpaces would be inside text data of the document. So mechanically removing all Spaces; \n \t \r is not the answer. It is task of the parser to skip extra spaces and other chars between tags, right?

  • Those two questions may help you : https://stackoverflow.com/questions/229310/how-to-ignore-whitespace-while-reading-a-file-to-produce-an-xml-dom https://stackoverflow.com/questions/11703635/strip-whitespace-and-newlines-from-xml-in-java – Imene Noomene Oct 24 '17 at 17:10
  • yes.. i am very upset. –  Oct 24 '17 at 19:55

0 Answers0