XML文件解析时忽略空格的问题???

为了让XML的可视化结构更清淅,常常在子元素前有许多空格,而一般分析XML文档时,把这些空格也当作是元素的内容了.如

1<system>
2<status>shut up</status>
3</system>

那么在分析元素system时,将子元素status前的空格也会当成元素的子结点.
特别是在输出这个结点时,子元素status前本来有两个空格,也会变成四个空格,如果多输出几次,那还会有更多的空格的.
我想让分析器在分析XML文档时忽略空格:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
这样建立的解析器,就应该忽略空格才对呀,可好像事实并非如此,这个解析器在分析XML文件时仍然没有忽略空格.
我应该怎么做???
---------------------------------------------------------------

you need a DTD,see
http://www.ibiblio.org/xml/books/xmljava/chapters/ch09s06.html

"... However, for this property to make a difference, the documents must have a DTD and should be valid or very nearly so. Otherwise the parser can’t tell which white space is ignorable and which isn’t. ..."

Published At
Categories with Web编程
Tagged with
comments powered by Disqus