Peguei um codigo que edita o xml e esta dando erro alguem saber me dizer o pq?
o erro que acusa é
[Fatal Error] connections.xml:1:2: The markup in the document preceding the root element must be well-formed.
org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at modifyXML.main(modifyXML.java:25)
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.OutputStream;
import java.io.StringWriter;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
public class modifyXML {
public static void main(String args[]) {
try {
File file = new File("connections.xml");
//Create instance of DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Get the DocumentBuilder
DocumentBuilder docBuilder = factory.newDocumentBuilder();
//Using existing XML Document
Document doc = docBuilder.parse(file);
//create the root element
Element root = doc.getDocumentElement();
//create child element
Element childElement = doc.createElement("number");
//Add the attribute to the child
childElement.setAttribute("id","3");
root.appendChild(childElement);
//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
OutputStream f0;
byte buf[] = xmlString.getBytes();
f0 = new FileOutputStream("connections.xml");
for(int i=0;i<buf .length;i++) {
f0.write(buf[i]);
}
f0.close();
buf = null;
}
catch(SAXException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
catch(ParserConfigurationException e) {
e.printStackTrace();
}
catch(TransformerConfigurationException e) {
e.printStackTrace();
}
catch(TransformerException e) {
e.printStackTrace();
}
}
}
xml
< ?xml version="1.0" encoding="UTF-8"?>
<connection>
<number id="1"/>
<number id="2"/>
</connection>