Results 1 to 1 of 1
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default 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:

    Java Code:
    Node Name:tag
    Value:null
    
    Node Name:#text
    Value:tagvalue
    and I would have expected this:
    Java Code:
    Node Name:tag
    Value:tagvalue
    So...
    My DTD (Note.dtd):
    Java Code:
    <!ELEMENT note (to,from,heading,body)>
    <!ELEMENT to (#PCDATA)>
    <!ELEMENT from (#PCDATA)>
    <!ELEMENT heading (#PCDATA)>
    <!ELEMENT body (#PCDATA)>
    My XML file (Note.xml):
    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>
    And my source code (XercesTestParser,java):
    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);
    			}
    		}
    	}
    }
    Output:
    Java 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
    Note all the nodes like
    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 Boyo
    Last edited by Singing Boyo; 08-18-2009 at 08: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

  1. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 08:08 PM
  2. nodes
    By Dr Gonzo in forum New To Java
    Replies: 1
    Last Post: 12-08-2008, 05:22 PM
  3. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 09:36 PM
  4. nodes in java
    By ahsan in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 04:09 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •