JavaLearner
JavaLearner

Reputation: 11

JDOM removing a element from root

I am not able to remove an element from root element. Below is the example of xml

 <ADMIN-DATA>
    <DATA-DECLARATION ID="Hi"> </DATA-DECLARATION>
    <DATA ID="Hi">
            <DATA-DECLARATION-REF ID-REF="Hi"> </DATA-DECLARATION-REF>
                 <DATA ID="Hi">
                        <DATA-DECLARATION ID="Delete"> </DATA-DECLARATION>
                 </DATA>
     </DATA>
 </ADMIN-DATA>

I want to delete

<DATA-DECLARATION ID="Delete"> </DATA-DECLARATION>

JDOM Code below

Element root = document.getRootElement();
        String id = null;
        boolean check = false;
        String idRef = null;
        ElementFilter filter = new org.jdom2.filter.ElementFilter(
                "DATA-DECLARATION");
        ElementFilter filter2 = new org.jdom2.filter.ElementFilter(
                "DATA-DECLARATION-REF");

        for (Element dataDecId : root.getDescendants(filter))
        {
            check = false;
            id = dataDecId.getAttributeValue("ID");
            for (Element dataDecIdRef : root.getDescendants(filter2))
            {
                idRef = dataDecIdRef.getAttributeValue("ID-REF");

                if (null != idRef && idRef.equalsIgnoreCase(id))
                {
                    check = true;
                    break;
                }
            }
            if (!check)
            {
                root.removeContent(dataDecId);

            }
        }

Above root.removeContent(dataDecId); is not working. Correct me.

Upvotes: 1

Views: 826

Answers (3)

Another way:

XPATH combines tag and values in a clear way:

XPath xPath = XPathFactory.newInstance().newXPath();
String expression="//DATA-DECLARATION[@ID='Delete']"; // self-explained

NodeList nodes  = (NodeList)  xPath.compile(expression).evaluate(document, XPathConstants.NODESET);

for(int i=0; i<nodes.getLength(); i++)
{
 Node the_node = nodes.item(i);

 if(the_node instanceof Element)
    {
     Element the_element=(Element) the_node;

    // FATHER
    Node father=the_node.getParentNode();
    // SUPPRESSION
    father.removeChild(the_node);

     // First one only ?
     break;
    }
}

Upvotes: 0

rolfl
rolfl

Reputation: 17707

Note that <DATA-DECLARATION ID="Delete"> </DATA-DECLARATION> is not a child of the root element.... it's a child of a DATA element which in turn is a child of a DATA element which finally is a child of the ADMIN-DATA element.

You cannot ask the root element to remove DATA-DECLARATION ID="Delete" because it is not a direct child.

Note that the child itself knows it's location, so, the simpler way to do it, is to change root.removeContent(dataDecId) to be just dataDecId.detach()

Upvotes: 1

LordAnomander
LordAnomander

Reputation: 1123

Well, to me it looks like there are some errors in the .xml file. You're trying to get the ID-REF field whereas Delete only has an ID.

Moreover, I doubt that your XML file is correct considering you have a typo here: <DATA-DECLARATION-REF ID-REF="Hi"> </ATA-DECLARATION-REF> and two different tags here: <DATA-DECLARATION ID="Delete"> </DATA-DECLARATION-REF>.

Upvotes: 0

Related Questions