80
rated 0 times
[
82]
[
2]
/ answers: 1 / hits: 13781
/ 11 Years ago, tue, january 7, 2014, 12:00:00
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!
More From » java