java - Append an element in a XML using DOM keeping the format -
i have xml this
<?xml version="1.0" encoding="utf-8" standalone="no"?> <empleado> <consultortecnico> <nombre>pablo</nombre> <legajo antiguedad="4 meses">7778</legajo> </consultortecnico> <cnc> <nombre>brian</nombre> <legajo antiguedad="1 año, 7 meses">2134</legajo> <sueldo>4268.0</sueldo> </cnc> </empleado>
what want read xml , append "sueldo" @ same level "nombre" , "legajo" in element "cnc". "sueldo" must "legajo" x 2
the code have appends "sueldo" can see in xml above not indent should, im using propierties indent (this xml created same way, using dom)
public class main { public static void main(string[] args) { try { file xml = new file("c:\\empleado.xml"); if (xml.exists() == true) { documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); document doc = db.parse(xml); string legajo = doc.getelementsbytagname("legajo").item(1).getfirstchild().getnodevalue(); element sueldo = doc.createelement("sueldo"); node valorsueldo = doc.createtextnode(string.valueof(float.valueof(legajo)*2)); sueldo.appendchild(valorsueldo); node cnc = doc.getelementsbytagname("cnc").item(0); cnc.appendchild(sueldo); domsource source = new domsource(doc); transformerfactory tf = transformerfactory.newinstance(); transformer t = tf.newtransformer(); t.setoutputproperty(outputkeys.indent, "yes"); t.setoutputproperty("{http://xml.apache.org/xslt}indent-amount","2"); fileoutputstream fos = new fileoutputstream("c:\\empleado.xml"); streamresult sr = new streamresult(fos); t.transform(source,sr); } else throw new exception("no hay archivo xml con ese nombre en el directorio"); } catch (exception e) { system.out.println(e.getmessage()); } }
}
thank in advance guys, i'll appreciate here!
assuming input file same output you've shown without sueldo
element, initial cnc
element has five child nodes far dom concerned
- the whitespace text node (newline , 4 spaces) between
<cnc>
,<nombre>
- the
nombre
element node - the whitespace text node (newline , 4 spaces) between
</nombre>
,<legajo
- the
legajo
element node - the whitespace text node (newline , two spaces) between
</legajo>
,</cnc>
you inserting sueldo
element after final text node, produces
<cnc> <nombre>brian</nombre> <legajo antiguedad="1 año, 7 meses">2134</legajo> <sueldo>4268.0</sueldo></cnc>
and indent
output property moves closing </cnc>
tag next line, aligned opening one. auto indentation right thing need remove all whitespace-only text nodes initial tree.
alternatively, forget auto-indentation , - instead of adding sueldo
last child of cnc
(after final text node), instead add newline-and-four-spaces text node after legajo
(i.e. before last text node) , add sueldo
element after that.
as alternative approach entirely, consider doing transformation in xslt rather using dom apis
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <!-- ignore whitespace-only text nodes in input --> <xsl:strip-space elements="*"/> <!-- , re-indent output --> <xsl:output method="xml" indent="yes" /> <!-- copy verbatim except otherwise specified --> <xsl:template match="@*|node()"> <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy> </xsl:template> <!-- cnc elements, add sueldo last child --> <xsl:template match="cnc"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> <sueldo><xsl:value-of select="legajo * 2" /></sueldo> </xsl:copy> </xsl:template> </xsl:stylesheet>
which either run using transformerfactory
api java code or using standalone command-line xslt processor.
Comments
Post a Comment