<manifest
xmlns="http://www.open.net/main"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.open.net/main http://schemas.open.net/sys.xsd">
对于前两个名字空间我用如下代码实现了
Element root=new Element("mainifest");
Document doc=new Document(root);
root.setNamespace(Namespace.getNamespace("","http://www.open.net/main"));
root.addNamespaceDeclaration(Namespace.getNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance"));
对于第三个名字空间xsi:schemaLocation="http://www.open.net/main http://schemas.open.net/sys.xsd"用jdom如何写呢??
Element root=new Element("mainifest");
Document doc=new Document(root);
root.setNamespace(Namespace.getNamespace("","http://www.open.net/main"));
NameSpace ns=Namespace.getNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
root.addNamespaceDeclaration(ns);
root.addAttribute(new Attribute("schemaLocation","http://www.open.net/main http://schemas.open.net/sys.xsd",ns));//第3个是属性,使用上面声明的NameSpace
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U ( http://www.altova.com) by () -->
<!--Sample XML file generated by XMLSpy v2005 rel. 3 U ( http://www.altova.com)-->
<root>
</root>
method1:
doc = new Document();//先NEW一个然后才能ADD
doc.addContent(new Comment("edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by ()"));
doc.addContent(new Comment("Sample XML file generated by XMLSpy v2005 rel. 3 U (http://www.altova.com)"));
method2:
Document doc = new Document();//先NEW一个然后才能ADD Comment c1 = new Comment("edited with XMLSpy v2005 rel. 3 U ( http://www.altova.com) by ()"); Comment c2 = new Comment("Sample XML file generated by XMLSpy v2005 rel. 3 U ( http://www.altova.com)"); Collection c = new ArrayList(); c.add(c1); c.add(c2); doc.addContent(c); Element root = new Element("root"); doc.setRootElement(root); Format format = Format.getPrettyFormat(); XMLOutputter output = new XMLOutputter(format); System.out.println(output.outputString(doc));