java - How to export JTree data as XML file by using JDOM -
i have jtree , need save each child level element save xml, tried using below code implement it, gives errors converting numeric data of xml document,
error message :
exception in thread "awt-eventqueue-0" org.jdom2.illegalnameexception: name "1001" not legal jdom/xml elements: xml name '1001' cannot begin character "1". @ org.jdom2.element.setname(element.java:227) @ org.jdom2.element.<init>(element.java:161) @ org.jdom2.element.<init>(element.java:173) error pointed to, element el = new element(node.tostring());
what may cause error?
// save xml file has been modified. private void savemsg(treemodel model) { filefilter filter = new filenameextensionfilter("xml files (*.xml)","xml"); filechooser.addchoosablefilefilter(filter); filechooser.setcurrentdirectory(new file("")); filechooser.setdialogtitle("save xml file"); if (filechooser.showsavedialog(frame) == jfilechooser.approve_option){ //file file = filechooser.getselectedfile(); treemodel tmodel = xmltree.getmodel(); int childcount = tmodel.getchildcount(tmodel.getroot()); logger.info("messages sent : " + childcount); for(int i=0; i<childcount; i++){ // go through each xml messages. jtree2xml(tmodel.getchild(tmodel.getroot(), i)); } } } // end of save xml file. private void jtree2xml(object node) { document doc = new document(); element root = createtree(doc, xmltree.getmodel(), node); doc.addcontent(root); xmloutputter out = new xmloutputter(format.getprettyformat()); try { out.output(doc, system.out); out.output(doc, new filewriter(file)); system.out.println("saved xml file..."); } catch (ioexception e) { e.printstacktrace(); logger.error("xml file saving error : "); } } // end of method. // read through document's elements. private static element createtree(document doc, treemodel model, object node) { **element el = new element(node.tostring());** (int = 0; < model.getchildcount(node); i++) { object child = model.getchild(node, i); el.addcontent(createtree(doc, model, child)); } // end of while loop. return el; }
the message jdom clear. element name cannot start character '1'. comes xml spec http://www.w3.org/tr/2004/rec-xml11-20040204/#nt-namestartchar
you should use fixed name element, , put jtree node tostring in attribute.
<jtreenode value="1001"/> rolfl
Comments
Post a Comment