0

我有一个 xml 文档,将用作模板

<?xml version="1.0" encoding="UTF-8" standalone="no"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><content type="application/xml"><m:properties><d:AccountEnabled>true</d:AccountEnabled><d:DisplayName>SampleAppTestj5</d:DisplayName><d:MailNickname>saTestj5</d:MailNickname><d:Password>Qwerty1234</d:Password><d:UserPrincipalName>saTestj5@identropy.us</d:UserPrincipalName></m:properties></content></entry>

我使用这个代码在 java 中调用它,其中 payLoadXML.xml 具有上述内容。

"InputStream is = getClass().getClassLoader().getResourceAsStream("/payLoadXML.xml");"

现在我正在尝试编辑标签值,例如将“saTestj5”更改为“saTestj6”,然后转换整个 xml 并将其存储在 xml 中。谁能告诉我如何实现这一目标?有人告诉我这可以通过使用“节点”来完成,这可能吗?

4

2 回答 2

0

使用 jaxb 或 sax 解析器通过使用 getter 方法转换为对象并更改对象并转换回 xml

试试这个

于 2013-02-25T14:48:55.673 回答
0
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = null;
        docBuilder = docFactory.newDocumentBuilder();

        Document doc = null;
        InputStream is = getClass().getClassLoader().getResourceAsStream("/payLoadXML.xml");
        doc = docBuilder.parse(is);

        Node staff = doc.getElementsByTagName("m:properties").item(0);

        Text givenNameValue = doc.createTextNode("abc");
        Element givenName = doc.createElement("d:GivenName");
        givenName.appendChild(givenNameValue);
       staff.appendChild(givenName);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        transformer = transformerFactory.newTransformer();

        DOMSource source = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);

        transformer.transform(source, result);
于 2013-02-25T19:12:23.050 回答