3

I want to remove the wrapper from the following XML document using the DOM api

<hs:PageWrapper>
    <div id="botton1"/>
    <div id="botton2"/>
</hs:PageWrapper>

so that I will only have these as the final output:

<div id="botton1"/>
<div id="botton2"/>

How can i do this in Java?

1
  • 3
    The output you want is not a valid XML document. You would have to create two separate documents and serialize them one after the other. Why do you want to produce an invalid document? Commented Nov 16, 2010 at 21:33

1 Answer 1

5

What you want to do will not result in well formed XML as there will be 2 elements at the document root. However, code to do what you want is below. It gets the child nodes of the wrapper element, creates a new document for each node, imports the node into the document and writes the document into a String.

    public String peel(String xmlString) {
    StringWriter writer = new StringWriter();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(
                xmlString)));
        NodeList nodes = document.getDocumentElement().getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            Document d = builder.newDocument();
            Node newNode = d.importNode(n, true);
            d.insertBefore(newNode, null);
            writeOutDOM(d, writer);
        }
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

protected void writeOutDOM(Document doc, Writer writer) 
     throws TransformerFactoryConfigurationError, TransformerException {
    Result result = new StreamResult(writer);
    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance()
            .newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    transformer.transform(domSource, result);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Thank you very much for that. I tried that and I got an error the following error : HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. this occured on line d.appendChild(newNode);
Hmmm. I tried it before I posted the answer, and it worked. I am using SUN JDK 6 on Linux. What are you using?
@Farouk, I have made a small change to the code. I replaced appendChild with insertBefore.
Hi thank you very much. I think the best way is to use dump the XML straight in the document without a root element as its creating an unnecessary overhead. Thanks for your help anyway. I think the solution you gave is conceptually correct so will mark it as such
Glad I could help. Do note, that for a well formed XML document, you can only have one element at the root level, so if you need to parse the document again, you'll need the root element.

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.