2012-09-18 47 views
1

我有这个XML文件:爪哇 - 编辑节点的元素depenting节点的属性

<Credentials> 
    <WebsiteApplication id="test"> 
     <Username>ADMIN</Username> 
     <Password>ADMIN</Password> 
    </WebsiteApplication> 
</Credentials> 
<Credentials> 
    <WebsiteApplication id="test2"> 
     <Username>ADMIN2</Username> 
     <Password>ADMIN2</Password> 
    </WebsiteApplication> 
</Credentials> 

,我想编辑“WebsiteApplication”节点的用户名(或密码),只有当它的id是相同的与一个给了一个输入..

我想有些事情这一点,但它不工作...

System.out.println("Insert the id:...."); 
    BufferedReader websiteIn = new BufferedReader(new InputStreamReader(System.in)); 
    String Editid = websiteIn.readLine().toString(); 


    Document doc = SecondaryFuncts.FindXML(); 

    Element root = doc.getDocumentElement(); 
    NodeList rootlist = root.getChildNodes(); 
    for(int i=0; i<rootlist.getLength(); i++) 
    { 


//THERE IS NO PROBLEM UNTILL NOW... 
//And now i trying to take the id and check if matches 
//with my input and them change nodes elements... 

     Element Applications = (Element)rootlist.item(i); 
     NamedNodeMap id = Applications.getAttributes(); 
     for(int ids = 0 ; ids <id.getLength(); ids++) 
     { 


I tried a lot in this loop but nothing worked.. what can i do here? 
Lets assume that i want username and password to change both as "test" 

     } 

回答

1

使用XPath这样的任务。您将直接选择正确的WebsiteApplication元素,然后您可以修改其UsernamePassword孩子。

使用以下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> 
+1

@SpDaglas看到我的编辑 – Alex

+0

由于某种原因,它不工作..我没有得到任何错误,所以也许xpath表达式ro找到具有特定id的节点不工作,因为我想..什么可能是错的..? – tequilaras

+0

@SpDaglas我添加了一个完整的代码示例 – Alex