0

Here i have xml structure as

<book>  <!--node 0-->
    <id>1111</id>
    <name>abacd</name>
    <author>abcd</author>
    <price>700</price>
    <category>abcd</category>       
</book>                       
<book>  <!--node 1-->
    <id>2222</id>
    <name>abacd</name>
    <author>abcd</author>
    <price>700</price>
    <category>abcd</category>       
</book>  
<book>  <!--node 2-->
    <id>3333</id>
    <name>abacd</name>
    <author>abcd</author>
    <price>700</price>
    <category>abcd</category>       
</book>  

Based on node number i need to delete that node entirely. I'm able to get node number and all child nodes but i dont know how to deltet,can anybody guide me how to do it.eg:node 0. has to delete. This is what i tried to delet node 0 :

File fXmlFile = new File(xmlfilePath);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    NodeList nList = doc.getElementsByTagName("book");
    Node nNode = nList.item(Integer.parseInt(nodeNumber));
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
         Element eElement = (Element) nNode;
        String id = eElement.getElementsByTagName("id").item(0).getTextContent();
         if ((id.equals(bookId))) {
            eElement.getElementsByTagName("id").item(0).removeChild(nNode);
        }
    }

please help me to get desired output.

khanam
  • 335
  • 1
  • 4
  • 19
  • here you go http://stackoverflow.com/questions/3717215/remove-xml-node-using-java-parser – ppuskar Dec 26 '14 at 11:50
  • @ppuskar,i dont want to use XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]"); please guide me – khanam Dec 26 '14 at 11:51

1 Answers1

0

Your XML is missing a root element. Here my corrected version:

<root>
<book>  <!--node 0 -->
    <id>1111</id>
    <name>abacd</name>
    <author>abcd</author>
    <price>700</price>
    <category>abcd</category>
</book>
<book>  <!--node 1 -->
    <id>2222</id>
    <name>abacd</name>
    <author>abcd</author>
    <price>700</price>
    <category>abcd</category>
</book>
<book>  <!--node 2 -->
    <id>3333</id>
    <name>abacd</name>
    <author>abcd</author>
    <price>700</price>
    <category>abcd</category>
</book>

Here a JUnit test using XMLBeam

public class DeleteNodeTest {

public interface Books {
    @XBDelete("//book[id=$PARAM0]")
    int deleteBooks(int id);
}

@Test
public void deleteNode() throws IOException {
    Books books = new XBProjector(Flags.TO_STRING_RENDERS_XML).io().url("resource://test.xml").read(Books.class);
    System.out.println("Deleted " + books.deleteBooks(1111) + " nodes.");
    System.out.println(books);
}

}

This program prints out:

Deleted 1 nodes.

<book>  <!--node 1 -->
    <id>2222</id>
    <name>abacd</name>
    <author>abcd</author>
    <price>700</price>
    <category>abcd</category>
</book>
<book>  <!--node 2 -->
    <id>3333</id>
    <name>abacd</name>
    <author>abcd</author>
    <price>700</price>
    <category>abcd</category>
</book>

Cfx
  • 2,272
  • 2
  • 15
  • 21