|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi,
This is a great forum with lots of bright developers' posts. I hope I can get some useful hints from here since I couldn't catch anything by google. My headache is that I don't know how to write some new items back to XML file which is parsed into a DOM tree using DOM API. For example, after a doc.createElement("elm")... how to save this new element to the XML file? I'm currently using java. Thanks in advance. ![]() |
|
#2
|
|||
|
|||
|
Hi,
u can use following code but u need to download jar file for jdom from http://www.jdom.org/dist/binary/ import java.io.*; import org.jdom.*; import org.jdom.output.*; public class CreateXML { public static void main(String argv[]) { Document document = createQuotesDocument(); outputDocument(document); } private static Document createQuotesDocument() { // Create the root element and the document. Element rootElement = new Element("quotes"); Document quoteDocument = new Document(rootElement); rootElement.addContent(new Comment("All quotes that are fitted to show.")); Element quote = new Element("quote"); quote.addContent("You will meet a JDOM Guru today."); quote.setAttribute(new Attribute("num", "1")); Element author = new Element("author"); author.addContent("Software Objects"); quote.addContent(author); rootElement.addContent(quote); rootElement.addContent("I want to eat some ice cream."); return quoteDocument; } // createQuotesDocument private static void outputDocument(Document document) { try { XMLOutputter outputter = new XMLOutputter(" ", true); outputter.output(document, System.out); } catch (java.io.IOException e) { e.printStackTrace(); } }//end of outputDocument }//end of class Thank You Tushar.. ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > How to write to XML using java DOM API? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|