This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DOM (xml) question  RSS feed

 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that cleared it right up.

Thanks a lot.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!