Results 1 to 1 of 1
- 08-18-2009, 06:57 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
XML Parsing, strange empty/duplicated Nodes
I'm trying to parse XML documents, using DTD validation, but I'm coming up with #text Nodes that have a newline and tabs, and I'm getting a root node with no children, along with a root node that has children. I'm using the Xerces parser, but I could throw something together using standard java packages if it would help.
It could be a problem with my DTD/XML file, or my source code, but I'm hoping someone can help me out. I don't really want to have to test for empty #text nodes and always have to remove the first child node.
Also, I'm getting things like this:
and I would have expected this:Java Code:Node Name:tag Value:null Node Name:#text Value:tagvalue
So...Java Code:Node Name:tag Value:tagvalue
My DTD (Note.dtd):
My XML file (Note.xml):Java Code:<!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
And my source code (XercesTestParser,java):Java Code:<!DOCTYPE note SYSTEM "Note.dtd"> <!-- Taken from Brother Bear --> <note> <to>Moose</to> <from>Moose2</from> <heading>I Spy Question</heading> <body>I spy something green</body> </note>
Output:Java Code:import java.io.IOException; import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; public class XercesTestParser { public static void main(String[] args){ DOMParser parser = new DOMParser(); try { parser.setFeature("http://xml.org/sax/features/validation", true); } catch (SAXNotRecognizedException e) { e.printStackTrace(); } catch (SAXNotSupportedException e) { e.printStackTrace(); } try { parser.parse("Note.xml"); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Document d = parser.getDocument(); d.getDocumentElement(); printNode(d,true); } private static void printNode(Node n,boolean recursive){ NodeList nl = n.getChildNodes(); System.out.println("Name: " + n.getNodeName()); System.out.println("Value: " + n.getNodeValue()); System.out.println("Parent: " + (n.getParentNode()==null?"No Parent":n.getParentNode().getNodeName())); System.out.println("Children: " + nl.getLength()); System.out.println(); if(recursive){ for(int i=0; i<nl.getLength(); i++){ printNode(nl.item(i),true); } } } }
Note all the nodes likeJava Code:Name: #document Value: null Parent: No Parent Children: 3 Name: note Value: null Parent: #document Children: 0 Name: #comment Value: Taken from Brother Bear Parent: #document Children: 0 Name: note Value: null Parent: #document Children: 9 Name: #text Value: Parent: note Children: 0 Name: to Value: null Parent: note Children: 1 Name: #text Value: Moose Parent: to Children: 0 Name: #text Value: Parent: note Children: 0 Name: from Value: null Parent: note Children: 1 Name: #text Value: Moose2 Parent: from Children: 0 Name: #text Value: Parent: note Children: 0 Name: heading Value: null Parent: note Children: 1 Name: #text Value: I Spy Question Parent: heading Children: 0 Name: #text Value: Parent: note Children: 0 Name: body Value: null Parent: note Children: 1 Name: #text Value: I spy something green Parent: body Children: 0 Name: #text Value: Parent: note Children: 0
Name: #text
Value:
These I can handle with some checking, but I would prefer not to. They also disappear if I put all my XML element tags on the same line.
Hopefully someone can tell me whether or not this can be fixed,
Singing BoyoLast edited by Singing Boyo; 08-18-2009 at 07:00 AM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
empty arrays.
By circuspeanuts in forum New To JavaReplies: 11Last Post: 04-06-2009, 07:08 PM -
nodes
By Dr Gonzo in forum New To JavaReplies: 1Last Post: 12-08-2008, 04:22 PM -
Empty ResultSet
By Java Tip in forum Java TipReplies: 0Last Post: 02-09-2008, 08:36 PM -
nodes in java
By ahsan in forum New To JavaReplies: 0Last Post: 12-26-2007, 03:09 PM -
BufferedReader empty
By Peter in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 06:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks