Skip to main content

[JAI-IMAGEIO] reading IIOMetadata

3 replies [Last post]
Anonymous

I am trying to read the name of the ColorSpaceType in the IIOMetadata XML of
a PNG image. The XML looks like

...

If I walk the DOM like this I get "RGB":

private String returnColor(Node node) {
NodeList gen1 = node.getChildNodes();
for (int i = 0; i < gen1.getLength(); i++) {
Node chroma = gen1.item(i);
if (chroma.getNodeName().equals("Chroma")) {
NodeList gen2 = chroma.getChildNodes();
for (int j = 0; j < gen2.getLength(); j++) {
Node colorSpace = gen2.item(j);
if (colorSpace.getNodeName().equals("ColorSpaceType")) {
NamedNodeMap map = colorSpace.getAttributes();
if (map != null) {
for (int k = 0; k < map.getLength(); k++) {
Node attr = map.item(k);
if (attr.getNodeName().equals("name"))
return attr.getNodeValue();
}
}
}
}
}
}

return "";
}

However, if I try to use XPath, I get an empty string although my expression
works when I load the file into an XPath tool like XPE:

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
try {
IIOMetadata metadata = reader.getImageMetadata(0);
Node metaRoot = metadata.getAsTree("javax_imageio_1.0");
// or "//ColorSpaceType/@name/text()"
String c = xpath.evaluate("//ColorSpaceType/@name", metaRoot);
System.out.println("color: '"+c+"' ("+imageColor+")");
} catch (XPathExpressionException e) {
e.printStackTrace(); // can't find color
}

Any ideas why IIOMetadata seems impervious to XPath?

--
"Hell hath no limits, nor is circumscrib'd In one self-place; but where we
are is hell, And where hell is, there must we ever be" --Christopher
Marlowe, *Doctor Faustus* (v, 121-24)
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Harald Kuhr

On 4. aug. 2010, at 21.33, Thad Humphries wrote:

> Any ideas why IIOMetadata seems impervious to XPath?

Disclaimer: I'm not an expert on XPath, but I take it that your XPath expression is correct. ;-)

It could be because IIOMetadata(Node) isn't really DOM, it just pretends to be.

>From the javadoc of IIOMetadataNode: "This class is not intended to be used for general XML processing. In particular, Element nodes created within the Image I/O API are not compatible with those created by Sun's standard implementation of the org.w3.dom API. In particular, the implementation is tuned for simple uses and may not perform well for intensive processing. Namespaces are ignored in this implementation. [...]"

So, XPath could be one of those areas where IIOMN is incompatible. Just a guess, though, but I have experience from other XML tools choking on the IIOMetadataNodes.

Best regards,

--
Harald K

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Thad Humphries

Thanks. That's a likely reason. However in that case IIOMetadata
getAsTree() should be declared to return a IIOMetadataNode, not a
org.w3.dom.Node.

On Thu, Aug 5, 2010 at 3:58 AM, Harald Kuhr wrote:

>
> On 4. aug. 2010, at 21.33, Thad Humphries wrote:
>
> > Any ideas why IIOMetadata seems impervious to XPath?
>
> Disclaimer: I'm not an expert on XPath, but I take it that your XPath
> expression is correct. ;-)
>
> It could be because IIOMetadata(Node) isn't really DOM, it just pretends to
> be.
>
> From the javadoc of IIOMetadataNode: "This class is not intended to be used
> for general XML processing. In particular, Element nodes created within the
> Image I/O API are not compatible with those created by Sun's standard
> implementation of the org.w3.dom API. In particular, the implementation is
> tuned for simple uses and may not perform well for intensive processing.
> Namespaces are ignored in this implementation. [...]"
>
> So, XPath could be one of those areas where IIOMN is incompatible. Just a
> guess, though, but I have experience from other XML tools choking on the
> IIOMetadataNodes.
>
>
> Best regards,
>
> --
> Harald K
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

--
"Hell hath no limits, nor is circumscrib'd In one self-place; but where we
are is hell, And where hell is, there must we ever be" --Christopher
Marlowe, *Doctor Faustus* (v, 121-24)
[att1.html]

Harald Kuhr

Hi,

Well.. Agreed. But an IIOMetadataNode is still a Node, so it should still behave like a Node (but it doesn't in many cases). To be honest, I never fully understood the rationale behind using the "DOM-like" approach for metadata. The argument that "developers are already familiar with DOM" doesn't really count, when the familiar DOM operations (like XPath in your case) doesn't work... I think it would be better to implenent a tree structure for image metadata from the ground up. But, I guess it's too late for that..

Anyway, good luck with what you're doing! :-)

--
Harald K

On 5. aug. 2010, at 16.50, Thad Humphries wrote:

> Thanks. That's a likely reason. However in that case IIOMetadata getAsTree() should be declared to return a IIOMetadataNode, not a org.w3.dom.Node.
>
> On Thu, Aug 5, 2010 at 3:58 AM, Harald Kuhr wrote:
>
> On 4. aug. 2010, at 21.33, Thad Humphries wrote:
>
> > Any ideas why IIOMetadata seems impervious to XPath?
>
> Disclaimer: I'm not an expert on XPath, but I take it that your XPath expression is correct. ;-)
>
> It could be because IIOMetadata(Node) isn't really DOM, it just pretends to be.
>
> From the javadoc of IIOMetadataNode: "This class is not intended to be used for general XML processing. In particular, Element nodes created within the Image I/O API are not compatible with those created by Sun's standard implementation of the org.w3.dom API. In particular, the implementation is tuned for simple uses and may not perform well for intensive processing. Namespaces are ignored in this implementation. [...]"
>
> So, XPath could be one of those areas where IIOMN is incompatible. Just a guess, though, but I have experience from other XML tools choking on the IIOMetadataNodes.
>
>
> Best regards,
>
> --
> Harald K
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>
>
>
> --
> "Hell hath no limits, nor is circumscrib'd In one self-place; but where we are is hell, And where hell is, there must we ever be" --Christopher Marlowe, Doctor Faustus (v, 121-24)

[att1.html]