...
>DocumentBuilderFactory.setIgnoringElementContentWhitespace doesn't
>seem to work at all, I still get the new lines as text elements :S
...
>Here the small code I used:
A highly motivated* master of the craft** might be able to spot
the mistake in your 63 line snippet by eye. To get the help
of 'the rest of us', you are better off posting an SSCCE***.
* Highly enough motivated to try and spot mistakes by
simply reading the code, as opposed to seeing the code
work/fail when run.
** ..and they would probably need to know XML processing
inside and out, often mistakes are spotted by people who do
not know an API that well, but were simply interested enough
to run a code sample.
*** <http://www.physci.org/codes/sscce.html>
It would be best to pull a small XML directly from
URL off a web site. If you cannot manage to upload
it to somehwere that is open to being fetched by Java,
try including a small sample in your post.

Signature
Andrew Thompson
http://www.athompson.info/andrew/
nuthinking@googlemail.com - 28 Sep 2007 08:06 GMT
The problem seemed it is that setIgnoringElementContentWhitespace
works if the xml refers to either to xsd or dtd.
Thanks anyway, chr
> >DocumentBuilderFactory.setIgnoringElementContentWhitespace doesn't
> >seem to work at all, I still get the new lines as text elements :S
[quoted text clipped - 24 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
Arne Vajhøj - 30 Sep 2007 22:13 GMT
> The problem seemed it is that setIgnoringElementContentWhitespace
> works if the xml refers to either to xsd or dtd.
To some extent that I think that makes sense.
Only with a DTD or XSD is it possible to identify something
as content whitespace.
Arne
Arne Vajhøj - 30 Sep 2007 22:37 GMT
>> The problem seemed it is that setIgnoringElementContentWhitespace
>> works if the xml refers to either to xsd or dtd.
[quoted text clipped - 3 lines]
> Only with a DTD or XSD is it possible to identify something
> as content whitespace.
Try look at the attached example.
Arne
====================================
package september;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.TreeWalker;
import org.xml.sax.InputSource;
public class XMLandWS {
public static void parse(String xml) throws Exception {
System.out.print(xml);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
TreeWalker walk = ((DocumentTraversal)
doc).createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_TEXT,
null, false);
Node n;
while ((n = walk.nextNode()) != null) {
System.out.println("=" + n.getNodeValue().replace("\n",
"\\n").replace(" ", "_"));
}
}
public static void main(String[] args) throws Exception {
parse("<all>\n" +
" <one>A</one>\n" +
" <one>BB</one>\n" +
" <one>CCC</one>\n" +
"</all>\n");
parse("<!DOCTYPE all [\n" +
"<!ELEMENT all (one)*>\n" +
"<!ELEMENT one (#PCDATA)>\n" +
"]>\n" +
"<all>\n" +
" <one>A</one>\n" +
" <one>BB</one>\n" +
" <one>CCC</one>\n" +
"</all>\n");
parse("<!DOCTYPE all [\n" +
"<!ELEMENT all (#PCDATA|one)*>\n" +
"<!ELEMENT one (#PCDATA)>\n" +
"]>\n" +
"<all>\n" +
" <one>A</one>\n" +
" <one>BB</one>\n" +
" <one>CCC</one>\n" +
"</all>\n");
}
}