How do I programmatically create XML from Java? -


i trying programmatically create xml elements using jaxb in java. possible? reading page here can use, have far found nothing.

usually start defining bean

@xmlrootelement public class myxml {    private string name;   public string getname() {  return name; }   @xmlelement public void setname(string s) { this.name = s; } } 

and serialize code like

public class serializer {    static public void main(string[] args) {       myxml m = new myxml();      m.setname("yo");      jaxbcontext jaxbcontext = jaxbcontext.newinstance(myxml.class);      marshaller jaxbmarshaller = jaxbcontext.createmarshaller();      jaxbmarshaller.setproperty(marshaller.jaxb_formatted_output, true);      jaxbmarshaller.marshal(m, new file("myxml_"+ ".xml"));   } } 

that whould produce following xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <myxml>     <name>yo</name> </myxml> 

how program java class create element tag name depending on entered in program? instance in example tag element called 'name'. how set @ runtime though? possible generics or other way?

the b in jaxb stands bean no, there's no way use jaxb without defining beans.

you want dinamically create xml take @ joox example (link full gist)

document document = joox.builder().newdocument(); element root = document.createelement("contacts"); document.appendchild(root);  (string name : new string[]{"john", "jessica", "peter"}) {   $(root).append(     $("contact"       , $("name", name)       , $("active", "true")     )   ); } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -