java - Recursively building an XML tree using dom4j without duplicate code for the root node -
i serializing tree structure xml data. tree looks like
company department employee employee my desired root node company, xml like
<?xml version="1.0" encoding="utf-8"?> <company> <department> <employee> ... </employee> </department> </company> after looking @ examples, i've decided i'll write this
public void writenodes(element root, nodebase node) throws xmlstreamexception { element elmt = root.addelement(node.getname()); (nodebase child : node.getchildren()) { writenodes(child); } } public void makexml(nodecompany root, string filename) { try { document = documenthelper.createdocument(); writenodes(???, root); writefile(document, filename); } catch (exception e) { e.printstacktrace(); } } i want root node used root, don't want write duplicate code treat root node separately children.
since there ever single company node in tree, can perform explicit check in writenodes method skip company nodes, doesn't nice.
is there way complete code without duplicating code?
hy,
for me method writenodes unnecessary, can this:
public void makexml(element rootelement, string filename) { try { //you must detach can add in new document after element rootelementcopied = (element) rootelement.clone(); document document = documenthelper.createdocument(); document.add(rootelementcopied); writefile(document, filename); } catch (exception e){ e.printstacktrace(); } }
Comments
Post a Comment