add contant to XML through JDOM -


i have been starting @ jdom. until have created file has name file.xml. add content in xml file, little bit insecure how that? fx be: - name - lastname - age

hope can me? best regards julie

    ppackage examplepackage; import java.io.file; import java.io.filenotfoundexception; import java.io.ioexception; import org.jdom2.document; import org.jdom2.element; import org.jdom2.jdomexception; import org.jdom2.input.saxbuilder; import org.jdom2.output.format; import org.jdom2.output.xmloutputter;  public class readxmlfile {      public static void main(string[] args) {         try {             write();             read();         }         catch (filenotfoundexception e){             e.printstacktrace();         }         catch(ioexception e) {             e.printstacktrace();         }         catch(jdomexception e) {             e.printstacktrace();         }     }          public static void read() throws jdomexception, ioexception {               saxbuilder reader = new saxbuilder();             document document = reader.build(new file("file.xml"));             xmloutputter xout = new xmloutputter(format.getprettyformat());             xout.output(document, system.out);         }          public static void write() throws filenotfoundexception, ioexception {    document document = new document();         element root = new element("document");         root.setattribute("file", "file.xml");         root.addcontent(new element("style"));         document.setrootelement(root);          element person = new element("person");                      person.setattribute("name", "mads");         } } 

print out in console:

<?xml version="1.0" encoding="utf-8"?> <document file="file.xml">   <style /> </document> 

in case should print out person name "mads" right?

in read method want change line:

system.out.println(document.getcontent());

to like:

 xmloutputter xout = new xmloutputter(format.getprettyformat());  xout.output(document, system.out); 

that print entire document, not tostring() on parts of it.

then, adding content.... in write() method:

  • you set root element with:

        document document = new document();     element root = new element("document");     root.setattribute("file", "file.xml");     root.addcontent(new element("style"));     document.setrootelement(root); 

    .there nothing wrong that, more commonly done like:

        element root = new element("document");     root.setattribute("file", "file.xml");     root.addcontent(new element("style"));     document document = new document(root); // note root constructor 
  • if want add additional content, example:

    for (int = 0; < 10; i++) {     element count = new element("count" + i);     root.addcontent(count); } 

    or

    element person = new element("person");         person.setattribute("name", "jon"); person.setattribute("lastname", "skeet"); person.setattribute("age", "37"); root.addcontent(person); 

update: finally, after have added content, need write file out disk:

xmloutputter xmlout = new xmloutputter(format.getprettyformat()); try (fileoutputstream fos = new fileoutputstream("file.xml")) {     xmlout.output(document, fos); } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -