Дело в том, что XML-документ может иметь только один корневой элемент. В выделенной строчке ты пытаешься добавить к документу второй. Вот так все работает:
Код | import java.io.FileWriter; import java.io.IOException;
import org.apache.xml.serialize.DOMSerializer; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.Element;
public class MyClass { public void createFile(String a, String b) throws IOException { FileWriter out = new FileWriter("test.xml"); org.apache.xerces.dom.DocumentImpl doc = new org.apache.xerces.dom.DocumentImpl(); Element root = doc.createElement("test"); doc.appendChild(root); Element text = doc.createElement("text"); text.appendChild(doc.createTextNode(a)); root.appendChild(text); Element numb = doc.createElement("text2"); numb.appendChild(doc.createTextNode(b)); root.appendChild(numb); OutputFormat format = new org.apache.xml.serialize.OutputFormat( doc, "windows-1251", true); format.setIndenting(true); DOMSerializer serealizer = new XMLSerializer( out, format); serealizer.serialize(doc); out.close(); }
public static void main(String[] args) throws IOException { MyClass m = new MyClass(); m.createFile("bla-bla-bla", "bbbbb"); }
}
|
|