我有一个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中。任何人都可以告诉我如何实现这一目标?有人告诉我这可以通过使用“节点”来实现吗?
答案 0 :(得分:0)
使用jaxb或sax解析器通过使用getter方法转换为对象并更改对象并转换回xml
尝试this
答案 1 :(得分: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);