marc3l
marc3l

Reputation: 2595

How to ignore whitespaces and new line feeds while parsing a XML file

how can I ignore whitespaces while parsing a XML file. It always calls the characters(...) method again, while after the end element a '\n' or '\r' is following, so it calls this method twice, instead of only once.

Upvotes: 2

Views: 2616

Answers (1)

forty-two
forty-two

Reputation: 12817

A SAXParser that is parsing a document against a DTD calls ignorableWhitespace() when it encounters whitespace in element content. For example, if this XML fragment

<ol>
   <li>one</li>
   <li>two</li>
</ol>

is parsed against this DTD fragment:

<!ELEMENT ol (li+)>
<!ELEMENT li (#PCDATA)>

the SAXParser would call characters(...) for "one" and "two", and ignorableWhitespace(...) for all the white space between the elements.

Note also that this applies only to parsing against a DTD. When using a Schema, ignorableWhitespace(...) is not called (even though the same kind of information is available).

Upvotes: 1

Related Questions