jdom读取xml

importjava.io.File;

importjava.io.FileOutputStream;

importjava.util.List;

importorg.jdom.Document;

importorg.jdom.Element;

importorg.jdom.input.SAXBuilder;

importorg.jdom.output.XMLOutputter;

publicclassDemo{

privatestaticStringfilea="c:\\A.xml";

privatestaticStringfileb="c:\\B.xml";

privatestaticStringfilec="c:\\合并后.xml";

publicstaticvoidmain(String[]args)throwsException{

SAXBuilderbuilderb=newSAXBuilder();

Documentdocumentb=builderb.build(newFile(fileb));

Elementrootb=documentb.getRootElement();

SAXBuilderbuildera=newSAXBuilder();

Documentdocumenta=buildera.build(newFile(filea));

Elementelementa=documenta.getRootElement();

List<Element>elementLista=elementa.getChildren();

for(Elementelement:elementLista){

if(element.getAttribute("name").equals("app_name")){

continue;

}

rootb.addContent((Element)element.clone());

}

XMLOutputteroutputter=newXMLOutputter("",true);

outputter.output(documentb,newFileOutputStream(filec));

}

}

相关推荐