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.