This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes DOM (xml) question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DOM (xml) question" Watch "DOM (xml) question" New topic
Author

DOM (xml) question

jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Whenever I build a Document by parsing an XML file, I have no problems getting the root node with a call to getDocumentElement().

However, if I construct a Document in my application, and I search for that Documents root node, the method returns a null.

Here is a complete app:






Just for good measure, here is the test.xml file:
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Document.createElement() creates an element without a parent. You have to attach that element to something before it's part of a DOM tree. In particular, you have to attach one Element to a Document to be its root element:



Now the output will be what you expect.


[Jess in Action][AskingGoodQuestions]
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Yeah, that cleared it right up.

Thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM (xml) question
 
Similar Threads
not able to create XML through DOM...
XPath
getting value with Sax/w3c dom
Convert set of data into XML document
validate schema in DOM