Filling template xml in memory using Java and JDOM? -


i want create xml template during runtime in java using jdom.

below sample template

<parent> <issues> <issue id="issue-x"> <summary></summary> <category></category> .. </issue> </issues> </parent> 

i want load template file using java + jdom , following

<parent> <issues> <issue id="issue-1"> <summary>test 1</summary> <category>cat 1</category>        .. </issue> <issue id="issue-2"> <summary>test 2</summary> <category>cat 2</category> .. </issue>  </issues> </parent> 

ideally want create more issue nodes , fill data db & save file

reason thought use template because there additional nodes under <issue> need fill db & thinking filling via template faster

can guide me on how done in java using jdom?

note: template adhere xsd haven't given here.

thanks in advance

edit: code snippet below

string sxmlpath = "d:\\ws\\issue_sample.xml"; documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder; dbuilder = dbfactory.newdocumentbuilder(); org.w3c.dom.document doc = dbuilder.parse(new file(sxmlpath)); dombuilder dombuilder = new dombuilder(); document xconfigurationdocument; xconfigurationdocument = dombuilder.build(doc);  xpathfactory xpfac = xpathfactory.instance();        xpathexpression<element> xelements = xpfac.compile("//ns:my-issue/ns:issues",filters.element(),null,namespace.getnamespace("ns", "http://www.myns.net/schemas/issue")); list<element> elements = xelements.evaluate(xconfigurationdocument); (element xissuesparent : elements) {   system.out.println(xissuesparent.getname());     element xcloneissue = null ;     (element xissuechild : xissuesparent.getchildren())   {       xcloneissue = xissuechild.clone();       system.out.println(xissuechild.getname());       xissuesparent.removecontent(xissuechild);   }    (int = 1; < 3; i++) {                xcloneissue.setattribute("id", "issue-" + i);       xissuesparent.addcontent(xcloneissue);   }  } xmloutputter xmloutput = new xmloutputter();  // display nice nice xmloutput.setformat(format.getprettyformat()); xmloutput.output(xconfigurationdocument, new filewriter("c:\\temp\\outputfile.xml")); 

i trying out in sample application

the problem face in loop (for (int = 1; < 3; i++)) after 1st following error the content has existing parent "issues"

obviously missing new clone.

my question how can handle of element , keep adding parent

if adhere xsd take @ org.jdom.input.dombuilder can parse dtd into.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -