0

Usually there no problem using Dom4J to get attribute values from XML Files

Also in XML Tools I can use xsl and Xpath to get the value of "use fill" from that SVG

<svg xmlns="http://www.w3.org/2000/svg" width="2250" height="2700" viewBox="0 0 1800 2160" color-interpolation-filters="sRGB" fill="none" overflow="visible" fill-rule="evenodd" stroke-linecap="square" stroke-miterlimit="3" xmlns:xlink="http://www.w3.org/1999/xlink">
<g transform="translate(363.17-1988.15)">
    <use fill="#f00" xlink:href="#1"/>
    <g transform="translate(72.59-8.504)">
        <use xlink:href="#9"/>
        <use xlink:href="#B"/>
        <use xlink:href="#A"/>
    </g>
    <text x="25.29" y="2143.97" fill="#000" font-family="Arial" font-size="8">SetAnnotations</text>
</g>

e.g. that works in xmlSpy

/svg/g/use/@fill or //g[text="SetAnnotations"]/use/@fill

That (and everything else I tried) failed in Java (which usually works)

    public static String getNodeValue(Element doc, String xpath){
    try{
        return doc.selectSingleNode(xpath).getText();
    }catch (Exception ex){
        log.debug("getNodeValue failed with " + ex.getMessage());
        return null;
    }
}

or

public static String getAttributeValue(Element doc, String xpath, String attributeName){
    try{
        Node node = doc.selectSingleNode(xpath);
        String value = node.valueOf(attributeName);
        return value;
    }catch (Exception ex){
        log.debug("getAttributeValue failed with " + ex.getMessage());
        return null;
    }
}

for some reasons I can just use 1.5 + dom4J but as svg is pretty old I would hope for a solution without additional libraries as batik

Any idea how to work correctly with svg's (reading and changing attributes) in Java ?

Thank in advance for any hint

user3732793
  • 1,699
  • 4
  • 24
  • 53

1 Answers1

0

I think you've been told already that SVG documents might have a default namespace.


But since I'm writing anyway: Your SVG document has a default namespace that you need to take into account. The following XPath expression will work:

/*[local-name()= 'svg']/*[local-name() = 'g']/*[local-name() = 'use']/@fill

and

//*[local-name() = 'g'][*[local-name() = 'text']="SetAnnotations"]/*[local-name() = 'use']/@fill

Or, even better, register or declare this namespace and prefix the element names in your path expression.

Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • thanks, for some odd reason only the first one brings back a result in Java. How would register and declare a namespace ? – user3732793 Jan 05 '15 at 16:44
  • @user3732793 Now the second one should work, too. I'm not familiar with Dom4J - we'll have to wait for someone who is. But perhaps it's similar to: http://stackoverflow.com/questions/14317384/dom4j-selectnodesarg-dont-give-list-of-nodes ? – Mathias Müller Jan 05 '15 at 16:51
  • YES, that worked well I hope I can fiddle the change of the attribute myself.Thanks a lot ! – user3732793 Jan 06 '15 at 08:27
  • and yes the other thread which you have mentioned talks about a similiar topic...sorry I did not find it before – user3732793 Jan 06 '15 at 09:01