Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
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

 Answers
0

Try 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 compiler
//XPathFactory xpf = XPathFactory.newInstance();
// XPath xpath = xpf.newXPath();

//XPath Query
// XPathExpression expr = xpath.compile(/);
//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


[#48906] Tuesday, January 7, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neildrews

Total Points: 166
Total Questions: 103
Total Answers: 85

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
neildrews questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Tue, Oct 12, 21, 00:00, 3 Years ago
Tue, Mar 23, 21, 00:00, 3 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
;