1

I have the following xml

<Root>
    <Page>
        <Object ref="a"></Object>
        <Object ref="b"></Object>
    </Page>
</Root>

I want to convert into

<Root>
    <Page>
        <ObjectGroup ref="a">
            <Object ref="a"/>
        </ObjectGroup>
        <ObjectGroup ref="b">
            <Object ref="b"/>
        </ObjectGroup>
    </Page>
</Root>

Basically I want to introduce a new parent node. I used the below way but it's introducing more parents

 private static void loadRoots(String xMLPath) {
        Document doc = Migrate.initTreeOpFromString(xMLPath);
        Element rootElement = doc.getDocumentElement();
        loadPage(rootElement);
        Migrate.finishTreeOp(doc, "D:\\a.xml");
    }

    private static void loadPage(Element root) {
        NodeList pageNodes = root.getChildNodes();
        for (int j = 0; j < pageNodes.getLength(); j++) {
            Node pageNode = pageNodes.item(j);
            if (pageNode.getNodeType() == Node.ELEMENT_NODE) {
                loadObject((Element) pageNode);
            }
        }
    }

    private static void loadObject(Element pageNode) {
        NodeList objectNodes = pageNode.getChildNodes();
        for (int j = 0; j < objectNodes.getLength(); j++) {
            Node objectNode = objectNodes.item(j);
            if (objectNode.getNodeType() == Node.ELEMENT_NODE) {
                Element objGroup = pageNode.getOwnerDocument().createElement("ObjectGroup");
                String ref = objectNode.getAttributes().getNamedItem("ref").getTextContent();
                objGroup.setAttribute("ref", ref);
                pageNode.removeChild(objectNode);
                objGroup.appendChild(objectNode);
                pageNode.appendChild(objGroup);
            }
        }
    }

But the output for this is

<Root>
    <Page>
        <ObjectGroup ref="b">
            <Object ref="b"/>
        </ObjectGroup>
        <ObjectGroup ref="a">
            <ObjectGroup ref="a">
                <ObjectGroup ref="a">
                    <Object ref="a"/>
                </ObjectGroup>
            </ObjectGroup>
        </ObjectGroup>
    </Page>
</Root>

which is not what I wanted. How do I solve this

3
  • I would use XSLT. You could define a template to do that wrapping. Commented Jun 29, 2016 at 12:13
  • @Fildor thanks but I got solution Commented Jun 29, 2016 at 12:15
  • 1
    If you used JAXB your life would be so much easier. Commented Jun 29, 2016 at 12:41

1 Answer 1

1

It's my mistake.I should have used insertBefore. That did the trick

private static void loadObject(Element pageNode) {
        NodeList objectNodes = pageNode.getChildNodes();
        for (int j = 0; j < objectNodes.getLength(); j++) {
            Node objectNode = objectNodes.item(j);
            if (objectNode.getNodeType() == Node.ELEMENT_NODE) {
                Element objGroup = pageNode.getOwnerDocument().createElement("ObjectGroup");
                String ref = objectNode.getAttributes().getNamedItem("ref").getTextContent();
                objGroup.setAttribute("ref", ref);
                pageNode.insertBefore(objGroup, objectNode);
                objGroup.appendChild(objectNode);
            }
        }
    }
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.