如何使用DOM解析器在此XML文件中添加元素? [英] How to add an element in this XML file using DOM parser?

查看:139
本文介绍了如何使用DOM解析器在此XML文件中添加元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何修改这个XML文件,

I want to know how can I modify this XML file,

<?xml version="1.0"?>
<nombre>
    <id>12345</id>
</nombre>

转换为使用Java中的DOM解析器的XML文件,

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>

我尝试过但不行,

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!

推荐答案

尝试此代码,我已经使用Xpath导航XML并创建了新的Node并附加到XML。

Try This Code,I have used Xpath to navigate XML and created the new Node and Appended to XML.

或者没有XPATH,你可以做到。
以下代码没有Xpath.Xpath代码在注释中

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();
    }

如果找不到文件,请检查您的XML文件的路径
谢谢

In case file not found,please check the path of your XML files Thank you

这篇关于如何使用DOM解析器在此XML文件中添加元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆