How to remove specific element from a XML by given tag name. AXIOM

1.1k views Asked by At

Folks,

I m just wondering how could I remove an xml element by given tag name from given xml using AXIOM xml process library.

so far i have successfully build document.

StAXOMBuilder builder = new StAXOMBuilder(stream);
OMElement documentBuilder = builder.getDocumentElement();

thanks in advance.

2

There are 2 answers

1
Andreas Veithen On BEST ANSWER
OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(stream);
OMElement documentElement = builder.getDocumentElement();
Iterator it = documentElement.getChildrenWithName(new QName("http://namespace", "elementName"));
if (it.hasNext()) {
    it.next();
    it.remove();
}
0
Roshan Wijesena On

Well, I found the solution

  StAXOMBuilder builder = new StAXOMBuilder(stream);
    OMElement documentBuilder = builder.getDocumentElement();

     Iterator allChildren = documentBuilder.getChildren();
     while (allChildren.hasNext()) {
        OMElement omElement = (OMElement) allChildren.next();
        String elementName = omElement.getQName().toString();

        if ("apimPayload".equals(elementName)) {                       
        omElement.detach();
        break;
        }
     }