2011-08-11 78 views
4

我有一個非常簡單的xml文件,我想創建一個簡單的函數從中刪除一個項目。這裏是我的xml文件:java xml刪除項目

<?xml version="1.0"?> 
<book> 
    <person> 
    <name>Person 1</name> 
    </person> 
    <person> 
    <name>Person 2</name> 
    </person> 
    <person> 
    <name>Person 3</name> 
    </person> 
    <person> 
    <name>Person 4</name> 
    </person> 
</book> 

我只是想調用一個方法從文件中刪除一個名字。我對XML不是很熟悉,但設法創建了一個讀寫器,但現在我無法創建一個從我的文件中刪除項目的方法。

當我說刪除我的意思是一個項目:

deleteItem("Person 3"); 

然後XML文件將變更爲:

<?xml version="1.0"?> 
    <book> 
     <person> 
     <name>Person 1</name> 
     </person> 
     <person> 
     <name>Person 2</name> 
     </person> 
     <person> 
     <name>Person 4</name> 
     </person> 
    </book> 

我做了什麼錯:

public static void removeName(String personName) throws ParserConfigurationException, IOException, SAXException{ 
     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
     Document doc = docBuilder.parse (new File("test.xml")); 

     NodeList nodes = doc.getElementsByTagName("person"); 
     for (int i = 0; i < nodes.getLength(); i++) {  
      Element person = (Element)nodes.item(i); 
      Element name = (Element)person.getElementsByTagName("name").item(0); 
      String pName = name.getTextContent(); 
      if(pName.equals(personName)){ 
       person.getParentNode().removeChild(person); 
      } 
     } 

    } 

回答

6

使用DOM:

public static void main(String[] args) throws Exception { 
    final String xml = "<book> " + 
     "<person> " + 
      "<name>Person 1</name> " + 
     "</person> " + 
     "<person> " + 
      "<name>Person 2</name> " + 
     "</person> " + 
     "<person> " + 
      "<name>Person 3</name> " + 
     "</person> " + 
     "<person> " + 
      "<name>Person 4</name> " + 
     "</person> " + 
     "</book>"; 

     DocumentBuilderFactory dbf = 
      DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     InputSource is = new InputSource(); 
     is.setCharacterStream(new StringReader(xml)); 

     Document doc = db.parse(is); 
     deletePerson(doc, "Person 3"); 
     printXML(doc); 
    } 

    public static void deletePerson(Document doc, String personName) { 
    // <person> 
    NodeList nodes = doc.getElementsByTagName("person"); 

    for (int i = 0; i < nodes.getLength(); i++) { 
     Element person = (Element)nodes.item(i); 
     // <name> 
     Element name = (Element)person.getElementsByTagName("name").item(0); 
     String pName = name.getTextContent(); 
     if (pName.equals(personName)) { 
     person.getParentNode().removeChild(person); 
     } 
    } 
    } 

    public static void printXML(Document doc) 
    throws TransformerException { 
    Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 

    StreamResult result = new StreamResult(new StringWriter()); 
    DOMSource source = new DOMSource(doc); 
    transformer.transform(source, result); 

    String xmlString = result.getWriter().toString(); 
    System.out.println(xmlString); 
    } 
1

第一選擇文字較好的項目。

要做到這一點,使用XPath語法:/書/人[名稱/文本()=「人3」]

之後,你將有節點,只需從其父將其刪除。

下面是代碼(未測試):

InputSource source = new InputSource(new FileInputStream(##your file##)); 

XPathFactory builder = XPathFactory.newInstance(); 
XPath xpath = builder.newXPath(); 

String label = "Person 3"; 
XPathExpression exp = xpath.compile("/book/person[name/text() = \"" + label +"\"]"); 

Node node = (Node) exp.evaluate(source, XPathConstants.NODE); 
node.getParentNode().removeChild(node); 
0

基本上你解析文檔並獲得元素,將其取下。
您可以使用javax.xml.parsers和javax.xml.transform包完成此操作。

File file = new File(xmlFile); 

xmlfile存儲xml文件的名稱。將文件作爲文檔讀取。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = factory.newDocumentBuilder(); 
    Document doc = builder.parse(xmlFile); 
    TransformerFactory tFactory = TransformerFactory.newInstance(); 
    Transformer tFormer = tFactory.newTransformer();<br> 

然後獲取元素並將其刪除,如下所示。

Element element = (Element)doc.getElementsByTagName(remElement).item(0); 
// Remove the node 
    element.getParentNode().removeChild(element);