0

I want to remove XML node from XML based on jQuery result. I have XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<list>
    <member name="James">
        <friendlist>
            <friend>0001</friend>
            <friend>0002</friend>
            <friend>0003</friend>
        </friendlist>
    </member>
    <member name="Jamie">
        <friendlist>
            <friend>0003</friend>
            <friend>0002</friend>
            <friend>0001</friend>
        </friendlist>
    </member>
    <member name="Katie">
        <friendlist>
            <friend>0001</friend>
            <friend>0003</friend>
            <friend>0004</friend>
        </friendlist>
    </member>
</list>

I want to remove complete <member> tag whose name is equal to 'james' so that XML should look like this

<?xml version="1.0" encoding="UTF-8"?>
    <list>

        <member name="Jamie">
            <friendlist>
                <friend>0003</friend>
                <friend>0002</friend>
                <friend>0001</friend>
            </friendlist>
        </member>
        <member name="Katie">
            <friendlist>
                <friend>0001</friend>
                <friend>0003</friend>
                <friend>0004</friend>
            </friendlist>
        </member>
    </list>

I am using following code to get node and delete it but getting same XML as an output. It is not deleting XML node instead giving same XML.

So, how to use xQuery and remove XML node from XML?

5
  • General approach: Deserialize (parse) the XML into an object, traverse the object and remove the elements you need to, then serialise back to XML. There are any number of libraries you can use to do this. Look up DOM and SAX parsers. Commented Oct 17, 2014 at 13:19
  • 2
    "I am using following code [...]". Which code? I don't see any. Commented Oct 17, 2014 at 13:30
  • Hi,Dan Temple Can you provide some link or example then it would be more clear to me Commented Oct 17, 2014 at 13:45
  • Are you sure that you mean Java, as you mention jQuery, which is actually JavaScript. Commented Oct 17, 2014 at 16:28
  • Yes , I am sure as I am using java parser to parse xml and using jquery to extract a node(reach a node). Commented Oct 18, 2014 at 9:06

1 Answer 1

1

I do things like this with XMLBeam:

public class RemoveNode {
    public interface Projection {
    @XBDelete("/list/member[@name='{0}']")
    Projection removeMember(String name);
}

    public void main(String[] args) {
    Projection projection = new XBProjector(Flags.TO_STRING_RENDERS_XML).io.file("yourfile.xml").read(projection.class);
    System.out.println("Before:"+projection);
    System.out.println("After:"+projection.removeMember("james");
    }
}

Not sure if reading the XML from a file meets your setup. But there are several ways to create projections (from IOStream, Resources, DOM Nodes, Strings...)

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

2 Comments

Thanks, to everyone for their suggestions, all suggestion in some way helped me to get what I wanted.
@DeepaliSingh: if this assistance was helpful, would you consider accepting it? To do so, find the tick mark to the left of it, and click it, so it turns green. This marks it as the answer to your question, and marks your question as resolved. It also rewards your helper a little bit for answering, and so encourages them to help other people too. Thanks.

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.