使用XPath這樣的任務。您將直接選擇正確的WebsiteApplication
元素,然後您可以修改其Username
和Password
孩子。
使用以下XPath選擇正確的元素/Credentials/WebsiteApplication[@id="XXX"]
其中XXX
是用戶提供的輸入。
然後,只需檢索該元素的子元素並更改用戶名/密碼內容。
String inputId = "test1";
String xpathStr = "//Credentials/WebsiteApplication[@id='" + inputId + "']";
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile(xpathStr);
Node node = (Node)expr.evaluate(doc, XPathConstants.NODE);
// node is the correct <WebsiteApplication> element
// do what you have to do with its children using node.getChildNodes()
// or you can even access directly the two elements
expr = xpath.compile(xpathStr + "/Username");
Node username = (Node)expr.evaluate(doc, XPathConstants.NODE);
// and set their values using the setTextContent() method
username.setTextContent("test-username");
expr = xpath.compile(xpathStr + "/Password");
Node password = (Node)expr.evaluate(doc, XPathConstants.NODE);
password.setTextContent("test-password");
看到這個完整的示例:
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class SO12477695 {
public static void main(String[] args) throws Exception {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader("<root>\r\n" + //
"<Credentials>\r\n" + //
" <WebsiteApplication id=\"test\">\r\n" + //
" <Username>ADMIN</Username>\r\n" + //
" <Password>ADMIN</Password>\r\n" + //
" </WebsiteApplication>\r\n" + //
"</Credentials>\r\n" + //
"<Credentials>\r\n" + //
" <WebsiteApplication id=\"test2\">\r\n" + //
" <Username>ADMIN2</Username>\r\n" + //
" <Password>ADMIN2</Password>\r\n" + //
" </WebsiteApplication>\r\n" + //
"</Credentials>\r\n" + //
"</root>")));
String inputId = "test2";
String xpathStr = "//Credentials/WebsiteApplication[@id='" + inputId + "']";
// retrieve elements and change their content
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile(xpathStr + "/Username");
Node username = (Node) expr.evaluate(doc, XPathConstants.NODE);
username.setTextContent("test-username");
expr = xpath.compile(xpathStr + "/Password");
Node password = (Node) expr.evaluate(doc, XPathConstants.NODE);
password.setTextContent("test-password");
// output the document
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
System.out.println(writer.toString());
// the document is now saved, you may want to save it in a file.
}
}
它輸出這樣的:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<Credentials>
<WebsiteApplication id="test">
<Username>ADMIN</Username>
<Password>ADMIN</Password>
</WebsiteApplication>
</Credentials>
<Credentials>
<WebsiteApplication id="test2">
<Username>test-username</Username>
<Password>test-password</Password>
</WebsiteApplication>
</Credentials>
</root>
@SpDaglas看到我的編輯 – Alex
由於某種原因,它不工作..我沒有得到任何錯誤,所以也許xpath表達式ro找到具有特定id的節點不工作,因爲我想..什麼可能是錯的..? – tequilaras
@SpDaglas我添加了一個完整的代碼示例 – Alex