0

I have an xml file like following

    <mxCell id="0"/>

    <mxCell id="1" parent="0"/><mxCell connectable="0" id="2" parent="1"value="a=5" vertex="1"></mxCell>

<mxCell id="3" parent="2"  value="op1" vertex="1"></mxCell>

<mxCell connectable="0" id="4" parent="1"  value="b=8" vertex="1"></mxCell>

<mxCell id="5" parent="4"  value="ip1" vertex="1"></mxCell>

<mxCell id="6" parent="4"  value="op1" vertex="1"></mxCell>

<mxCell connectable="0" id="7" parent="1"  value="c=_x+_y" vertex="1"></mxCell>

<mxCell id="8" parent="7"  value="ip1" vertex="1"></mxCell>

from the xml file i want to get attribute value of "id" where attribute value of " parent="4" ". Though i know to simply retrieve the attribute values but i want to retrieve the attribute value by the attribute value of another attribute. How can i do this using xml parser in JAVA?

1
  • Loop throught nodes ('mxCell') and search for each 'parent' value, when '4' is found, get the 'id' in a list (or better, get the node in a list). Commented Mar 31, 2016 at 7:06

2 Answers 2

1

i have done it as following

Element eElement=(Element)nNode;
if(eElement.getAttribute("parent").equals("4")
{
System.out.println("id"=+eElement.getAttribute("id"));
}
Sign up to request clarification or add additional context in comments.

1 Comment

If this has to be part of processing the document with a parser, rather than just obtaining the value, that's pretty much the most straightforward and efficient approach.
0

You've got several options.

One is to turn your XML into a DOM (Document Object Model) and use an XPath expression on it to get the desired value. JDOM might be a good fit. See this question and its answer for examples: Select a node using xpath and jdom

The XPath expression you'd need is //mxCell[@parent='4']/@id. Note that if there's a default namespace defined in your XML document (you've provided an extract, not the whole document, so I can't tell) you'd need to bind that to a prefix and use it in the XPath expression.

Another option that won't require any external dependencies is to use XSLT. Check package javax.xml.transform for more info. A stylesheet that would output only the value you want is provided here:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" />

    <xsl:template match="node()|@*">
        <xsl:apply-templates select="node()|@*" />
    </xsl:template>

    <xsl:template match="//mxCell[@parent='4'][1]">
        <xsl:value-of select="@id" />
    </xsl:template>

</xsl:stylesheet> 

Note that this will only output the id attribute of the first mxCell element with a parent attribute that has value 4. That's what the [1] does at the end of that XPath expression.

If the value to search for is dynamic (instead of always 4) I suggest using an XSLT parameter to pass it to the transformer.

For parsing the output of the XSLT and dealing with multiple values, I leave it up to you. It should be simple to continue on from here. Note that the XSLT approach will probably be the most performant. JDOM is a fine library but for large documents the overhead will be quite significant, so memory use may become an issue.

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.