Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Java in General
DOM (xml) question
jefff willis
Ranch Hand
Posts: 113
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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:
package testing; import javax.xml.parsers.*; import org.xml.sax.*; import java.io.*; import org.w3c.dom.*; public class DocTest { public DocTest() { Document fileDocument = buildDocument ("test.xml"); Document appDocument = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Element root; appDocument = builder.newDocument(); root = createXMLElement(appDocument, "testRoot"); root.appendChild( createXMLElement(appDocument, "testData", "goes here") ); } catch(ParserConfigurationException ex) { System.err.println("Failed to create CXB document: " + ex); } catch(DOMException de) { System.err.println("(creating CXB) DOM Exception: " + de); } System.out.println("app XML root = " + showRoot( appDocument ) ); System.out.println("file XML root = " + showRoot( fileDocument ) ); } public String showRoot( Document doc) { Element root = doc.getDocumentElement(); if (root == null) return "(null)"; return root.toString(); } public static Document buildDocument( String fileName ) { Document theDocument; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); theDocument = builder.parse( new File(fileName) ); return theDocument; } catch (SAXException sxe) { // Error generated during parsing Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { System.err.println("Parser config error:" + pce); // Parser with specified options can't be built pce.printStackTrace(); } catch (IOException ioe) { System.err.println("IOException: " + ioe); // I/O error //ioe.printStackTrace(); } return null; } // build document public static Element createXMLElement( Document doc, String name, String text) { Element e = doc.createElement(name); Text t = doc.createTextNode( text ); e.appendChild(t); return e; } private Element createXMLElement( Document doc, String name) { Element e = doc.createElement(name); return e; } public static void main(String[] args) { DocTest dt = new DocTest(); } }
Just for good measure, here is the test.xml file:
<testRoot> <testData> goes here </testData> </testRoot>
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
I like...
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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:
appDocument = builder.newDocument(); root = createXMLElement(appDocument, "testRoot"); root.appendChild( createXMLElement(appDocument, "testData", "goes here") ); [B]appDocument.appendChild(root);[/B]
Now the output will be what you expect.
[Jess in Action]
[AskingGoodQuestions]
jefff willis
Ranch Hand
Posts: 113
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yeah, that cleared it right up.
Thanks a lot.
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
not able to create XML through DOM...
Convert set of data into XML document
XPath
getting value with Sax/w3c dom
validate schema in DOM
More...