Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java XML Document Problem

  • 29-05-2006 12:37PM
    #1
    Registered Users, Registered Users 2 Posts: 29


    Hi,
    I'm having a problem when I create an xml
    document and set the appropriate attributes I'm getting a 'null'
    output. The code and output are as follows:
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    
    Element root = doc.createElement("status_tracking");
    root.setAttribute("string","test");
    System.out.println(root);
    
    ==================================

    Outputs: [status_tracking: null]

    Any help appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    That code works fine for me on 1.4.2 JVM.

    What JVM are you using and make sure your Imports are correct.


  • Registered Users, Registered Users 2 Posts: 29 steve04


    Thanks hobbes,

    The code is running on Java 1.4.

    Are you getting the same debug displayed as: "[status_tracking: null]" or is the value that was inserted into the node being displayed?


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    I get this as output...
    <status_tracking string="test" />
    

    Here is the code I did.
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    
    public class docXmlSample {
    
    	public static void main(String[] args) throws Exception {
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    		Document doc = docBuilder.newDocument();
    
    		Element root = doc.createElement("status_tracking");
    		root.setAttribute("string","test");
    		System.out.println(root);
    
    	}
    
    }
    


  • Registered Users, Registered Users 2 Posts: 29 steve04


    Thanks Hobbes,
    I realised i was missing a jar when i saw you were getting the correct output :)


Advertisement