JDOM创建xml

    技术2022-05-19  20

    import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.XMLOutputter; public class xmlCreater { public static void main(String[] args) { new xmlCreater().creat("c://0303test.xml"); } public void creat(String fileName){ Element root = new org.jdom.Element("root"); Document doc=new Document(); doc.setRootElement(root); Element childselement = new org.jdom.Element("childs"); childselement.setAttribute("name","childs"); Element childelement = new org.jdom.Element("child"); childelement.setAttribute("name","child"); childselement.addContent(childelement); Element childs=root.addContent(childselement); Element parentsselement = new org.jdom.Element("parents"); parentsselement.setAttribute("name","parents"); Element parents=root.addContent(parentsselement); XMLOutputter outputter = new XMLOutputter(); try { outputter.output(doc, new FileOutputStream(fileName)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 


    最新回复(0)