I want to know how can I modify this XML file,
<?xml version="1.0"?>
<nombre>
<id>12345</id>
</nombre>
into a XML file like this using DOM parser in Java,
<?xml version="1.0" encoding="UTF-8" ?>
<heat>2013-09-09</heat>
<nombre>
<id>12345</id>
</nombre>
I have tried this but doesn't work,
public class Test {
public static final String xmlFilePath = "src/vnx.xml";
public static final String xml2FilePath = "src/input2.xml";
public static void main(String argv[]) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlFilePath);
Element version = document.createElement("heat");
version.appendChild(document.createTextNode("2013-09-09"));
document.appendChild(version);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(xml2FilePath));
transformer.transform(domSource, streamResult);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
}
It returns a parsing error. Any suggestion would be really helpful. Thanks a lot!
I want to know how can I modify this XML file,
<?xml version="1.0"?>
<nombre>
<id>12345</id>
</nombre>
into a XML file like this using DOM parser in Java,
<?xml version="1.0" encoding="UTF-8" ?>
<heat>2013-09-09</heat>
<nombre>
<id>12345</id>
</nombre>
I have tried this but doesn't work,
public class Test {
public static final String xmlFilePath = "src/vnx.xml";
public static final String xml2FilePath = "src/input2.xml";
public static void main(String argv[]) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlFilePath);
Element version = document.createElement("heat");
version.appendChild(document.createTextNode("2013-09-09"));
document.appendChild(version);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(xml2FilePath));
transformer.transform(domSource, streamResult);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
}
It returns a parsing error. Any suggestion would be really helpful. Thanks a lot!
Share Improve this question edited Jan 8, 2014 at 0:00 user3167333 asked Jan 7, 2014 at 23:44 user3167333user3167333 2573 gold badges4 silver badges8 bronze badges 1- I've already edited my question. Thanks! – user3167333 Commented Jan 8, 2014 at 0:01
3 Answers
Reset to default 1Try This Code,I have used Xpath to navigate XML and created the new Node and Appended to XML.
Or Without XPATH you can Do it. Following Code is without Xpath.Xpath Code is in Comment
try {
File inputFile = new File("src/vnx.xml");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// creating input stream
Document doc = builder.parse(inputFile );
//Xpath piler
//XPathFactory xpf = XPathFactory.newInstance();
// XPath xpath = xpf.newXPath();
//XPath Query
// XPathExpression expr = xpath.pile("/");
//Node attributeElement = (Node) expr.evaluate(doc, XPathConstants.NODE);
//New Node
Node childnode=doc.createElement("heat");
doc .appendChild(childnode);
childnode.setTextContent("12-34-56");
// writing xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
File outputFile = new File("src/input2.xml");
StreamResult result = new StreamResult(outputFile );
// creating output stream
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
In case file not found,please check the path of your XML files Thank you
Before reading your code, I found problems in your XML file.
A xml file should contains only one root, in your case, the correct file may be something like this:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<heat>2013-09-09</heat>
<nombre>
<id>12345</id>
</nombre>
</root>
Try This one:
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
/* create root elements */
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("root");
doc.appendChild(rootElement);
/* create elements */
Element heat= doc.createElement("Heat");
heat.setAttribute("heat", "2013-09-09");
rootElement.appendChild(heat);
Element number= doc.createElement("number");
rootElement.appendChild(number);
/* create child node and add id elements */
Element id= doc.createElement("ID");
id.setAttribute("id", "12345");
number.appendChild(id);
/* write the content into xml file */
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
/* set source and result */
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("input2.xml"));
transformer.transform(source, result);
return true;
} catch (ParserConfigurationException pce) {
LOGGER.error("exception while creating Xml File", pce);
return false;
} catch (TransformerException tfe) {
LOGGER.error("exception while creating Xml File", tfe);
return false;
} catch (Exception e) {
LOGGER.error("exception while creating Xml File", e);
return false;
}