1

我对 XML 和 JDOM 真的很陌生,所以我有一个菜鸟问题,很抱歉。我有一个 XML 文件,我想在其中插入值。我的 XML 文件是这样的;

<?xml version="1.0"?>
<message>
    <header>
        <messageType>  </messageType>
        <sendFrom> </sendFrom>
        <HostName> </HostName>
        <sendTo> </sendTo>
        <receiverName> </receiverName>
        <date> </date>
    </header>
    <body>
    </body>
</message>

所以我想要的是例如在之间添加值,并且<sendTo> </sendTo>我想在. 你能告诉我怎么做吗?<A></A><body> </body>

非常感谢。

4

3 回答 3

1

如果你使用dom,你可以这样做;

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(inputFile);

        Node messageType= doc.getElementsByTagName("messageType").item(0);//zero tells the order in the xml
        messageType.setTextContent("SMS");
于 2011-09-30T13:23:38.727 回答
0

我建议使用 XStream 进行 XML 处理。这里是 2 分钟教程的链接:http: //x-stream.github.io/tutorial.html

于 2011-09-30T13:26:03.313 回答