Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
XML and Related Technologies
How do I turn a string into a DOM Object
Stephan Wissel
Greenhorn
Posts: 9
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I've seen this:
http://forum.java.sun.com/thread.jspa?threadID=544465
So I coded that:
public class Test { /** * @param args * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { Test x = new Test(); x.execute("http://blizz.lotususers.sg/web20config.nsf/viewconfigurations.xml/Demoname"); } public void execute(String uri) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); DocumentBuilder b = f.newDocumentBuilder(); Document d = b.parse(uri); System.out.print(d.toString()); Document d2 = b.parse(new InputSource(new StringReader("<test>Bla<rot>blau</rot></test>"))); System.out.print(d2.toString()); } }
And it returns this:
[#document: null][#document: null]
What step do I miss to get the document?
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You are in fact getting the Document. What is confusing you is the result of the toString() method of Document.
Study the excellent table in the Javadocs for the org.w3c.dom.Node interface.
This reveals that the nodeValue for a Document Node is indeed null.
Knowledge of this table will help avoid many confusing results when working with the DOM.
Bill
Stephan Wissel
Greenhorn
Posts: 9
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I guess you refer to this one:
http://www.docjar.com/docs/api/org/w3c/dom/Node.html
Thank you for the pointer. So instead of
doc.toString()
I need to do:
getString(Document doc) { StreamResult result = null; ByteArrayOutputStream byteResult = new ByteArrayOutputStream(); try { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); if (doc != null){ logger.debug("Document is Not Null"); }else{ logger.debug("Document is Null"); } DOMSource source = new DOMSource(doc); result = new StreamResult(byteResult); transformer.transform(source, result); } catch (TransformerConfigurationException e) { logger.error("TransformerConfigurationException ", e); } catch (TransformerException e) { logger.error("TransformerException ", e); } return byteResult.toString(); }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Nested CDATA Section
'reading' an xml document - what am i doing wrong?
Printing values from a xml file
Problems validating XML document against schema document
Parse XML files with SAXParser: how to get "subnodes"
More...