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.

2
  • here you go stackoverflow.com/questions/3717215/… Commented Dec 26, 2014 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 Commented Dec 26, 2014 at 11:51

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.