SOP
SOP

Reputation: 801

Xml conditional parsing using java

I want to extract the only selected tags out of a XML file:

<Shape>
    <ShapeType>H2</ShapeType>
    <Annotation>
        <Properties>
            <PropertyValue PropertyName="field_label">label.modelSeriesCd</PropertyValue>
            <PropertyValue PropertyName="ContainerType">conditionContainer</PropertyValue>
        </Properties>
    </Annotation>
    <FootnoteNumber>1</FootnoteNumber>
    <Name>label.modelSeriesCd</Name>
    <Rectangle>
        <Rectangle X="14" Y="94" Width="43" Height="12" />
    </Rectangle>
</Shape>
<Shape>
    <ShapeType>H2</ShapeType>
    <Annotation>
        <Properties>
            <PropertyValue PropertyName="field_label">label.modelSeriesMd</PropertyValue>
            <PropertyValue PropertyName="ContainerType">mContainer</PropertyValue>
        </Properties>
    </Annotation>
    <FootnoteNumber>1</FootnoteNumber>
    <Name>label.modelSeriesCd</Name>
    <Rectangle>
        <Rectangle X="14" Y="94" Width="43" Height="12" />
    </Rectangle>
</Shape>

I want to extract only those tags which has "conditionContainer" as the value of "propertyValue" and all the tags inside tag I am trying below code :

private static void visitChildNodes(NodeList nList)
{
    for (int index = 0; index < nList.getLength(); index++)
    {
        Node node = nList.item(index);
        if (node.getNodeType() == Node.ELEMENT_NODE)
        {
            if(node.getNodeName().equalsIgnoreCase("shape"))
                System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());

please suggest me a way to do this.

Upvotes: 1

Views: 390

Answers (1)

Shar1er80
Shar1er80

Reputation: 9041

I suggest a recursive search through your Document object, since what you're looking for is a few levels deep.

Make a function that recursively calls itself passing what node you're currently on along with what tag you're looking and the value that tag must have.

Something like...

public static void main(String[] args) throws Exception {
    String xml
            = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<Shapes>\n"
            + " <Shape>\n"
            + "     <ShapeType>H2</ShapeType>\n"
            + "     <Annotation>\n"
            + "         <Properties>\n"
            + "             <PropertyValue PropertyName=\"field_label\">label.modelSeriesCd</PropertyValue>\n"
            + "             <PropertyValue PropertyName=\"ContainerType\">conditionContainer</PropertyValue>\n"
            + "         </Properties>\n"
            + "     </Annotation>\n"
            + "     <FootnoteNumber>1</FootnoteNumber>\n"
            + "     <Name>label.modelSeriesCd</Name>\n"
            + "     <Rectangle>\n"
            + "         <Rectangle X=\"14\" Y=\"94\" Width=\"43\" Height=\"12\" />\n"
            + "     </Rectangle>\n"
            + " </Shape>\n"
            + " <Shape>\n"
            + "     <ShapeType>H2</ShapeType>\n"
            + "     <Annotation>\n"
            + "         <Properties>\n"
            + "             <PropertyValue PropertyName=\"field_label\">label.modelSeriesMd</PropertyValue>\n"
            + "             <PropertyValue PropertyName=\"ContainerType\">mContainer</PropertyValue>\n"
            + "         </Properties>\n"
            + "     </Annotation>\n"
            + "     <FootnoteNumber>1</FootnoteNumber>\n"
            + "     <Name>label.modelSeriesCd</Name>\n"
            + "     <Rectangle>\n"
            + "         <Rectangle X=\"14\" Y=\"94\" Width=\"43\" Height=\"12\" />\n"
            + "     </Rectangle>\n"
            + " </Shape>\n"
            + "</Shapes>";

    Document xmlDocument = DocumentBuilderFactory
            .newInstance()
            .newDocumentBuilder()
            .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

    Node node = findPropertyTagAndValue(xmlDocument.getFirstChild(), "PropertyValue", "conditionContainer");
    if (node != null) {
        System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());
    }
}

public static Node findPropertyTagAndValue(Node node, String propertyTag, String propertyValue) {
    if (node == null) {
        // The node we're looking for does not exist
        return null;
    } else if (node.getNodeType() != Node.ELEMENT_NODE) {
        // Move to the next sibling node
        return findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
    } else if (node.getNodeName().equalsIgnoreCase(propertyTag) && node.getTextContent().equalsIgnoreCase(propertyValue)) {
        // We found the node we are looking for
        return node;
    } else if (node.hasChildNodes()) {
        // Check into the child nodes
        Node childNode = findPropertyTagAndValue(node.getFirstChild(), propertyTag, propertyValue);
        if (childNode == null) {
            // Nothing found in child node, so move to next sibling
            childNode = findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
        }
        return childNode;
    } else {
        // Move to the next sibling
        return findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
    }
}

Results:

Node Name = PropertyValue; Value = conditionContainer

Upvotes: 2

Related Questions