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
Generating XML from java
Maha Hassan
Ranch Hand
Posts: 133
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I am wondering if there is a way to generate the xml from
java
i mean that after creating an element using this code
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import java.io.File; import java.io.IOException; import org.w3c.dom.Document; import org.w3c.dom.DOMException; import org.w3c.dom.Element; class xml{ public static void main (String args[]){ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); // Create from whole cloth Element root = (Element) document.createElement("rootElement"); document.appendChild(root); root.appendChild( document.createTextNode("Some") ); root.appendChild( document.createTextNode(" ") ); root.appendChild( document.createTextNode("text") ); System.out.println(document); } catch(Exception e){ System.out.println(e); } } }
system.out.println(element);
i get <element>text of element</element>
or do i have to type them each time like
system.out.println("<"+element.getTag +">"+element.getTextNode()+"</");
etc,,
thanks
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
If you have a Document object in memory you can use a Transformer to output it as a stream to a file or buffer in memory.
See these classes:
import javax.xml.transform.* ; import javax.xml.transform.dom.* ; import javax.xml.transform.stream.StreamSource ; import javax.xml.transform.stream.StreamResult ;
Maha Hassan
Ranch Hand
Posts: 133
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks, that was very helpful
[ August 02, 2006: Message edited by: Maha Hassan ]
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
XML element
DOM (xml) question
Invalid XML error...Newbie question
Getting XML Out of a org.w3c.dom.Document
Can anyone help?I still can't append records in xml...
More...