SOP
SOP

Reputation: 801

xml creation using java

I want to iterate over a XML given below:

                <Annotation>
                    <Properties>
                        <PropertyValue PropertyName="field_label">label.modelSeriesCd</PropertyValue>
                        <PropertyValue PropertyName="ContainerType">conditionContainer</PropertyValue>
                    </Properties>
                </Annotation>

I am trying these codes: 1)

while(currentNode.hasChildNodes()){
                        System.out.println(currentNode.getNextSibling());
                        currentNode=currentNode.getNextSibling();
                    }

2)

for (int x = 0; x < childNode.getLength(); x++) {
                    Node current = childNode.item(x);
                    if (Node.ELEMENT_NODE == current.getNodeType()) {
                        String cN = current.getNodeName();
                        System.out.print(cN +" = ");
                        String cV = current.getNodeValue();
                        System.out.print(cV+" : ");
                        String cT = current.getTextContent();
                        System.out.println(cT);
                    }
                }

output:

[Shape: null]
ShapeType = null : H2
Annotation = null : 

                                label.modelSeriesCd
                                conditionContainer

I want output with all tag names and tag values i.e. it should display like: Properties PropertyValue PropertyName "field_label" value label.modelSeriesCd means I want output with all tags ,attribute name, attribute values and text values. so that I can write it in another XML

Upvotes: 2

Views: 58

Answers (1)

Sharon Ben Asher
Sharon Ben Asher

Reputation: 14383

The method below recurse over an XML tree, printing the requested info:

public static void printNodeTree(Node n) {
    // print XML Element name:
    System.out.println("ELEM: " + n.getNodeName());

    // print XML Element attributes:
    NamedNodeMap attrs = n.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++ ) {
            Node attr = attrs.item(i);
            System.out.println("ATTR: " + attr.getNodeName() + " = " + attr.getNodeValue());
        }
    }

    NodeList nodeList = n.getChildNodes();

    // print XML Element text value
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.getNodeType() == Node.TEXT_NODE) {
            String text = currentNode.getNodeValue();
            if (text != null && text.matches("\\S+")) {
                System.out.println("TEXT: " + text);
            }
        }
    }

    // recurse over child elements 
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            printNodeTree(currentNode);
        }
    }
}

You can initiate the traversal from the document root:

Document doc = ...
printNode(doc.getDocumentElement());

Output for the given input:

ELEM: Annotation
ELEM: Properties
ELEM: PropertyValue
ATTR: PropertyName = field_label
TEXT: label.modelSeriesCd
ELEM: PropertyValue
ATTR: PropertyName = ContainerType
TEXT: conditionContainer

Upvotes: 1

Related Questions