Checking and deleting attributes in SVG using Bati

2019-08-10 02:54发布

问题:

The question basically says it all. How can I check if SVG has a viewBox attribute? I am using Batik lib. I need this because I need to (at least) notify the user that there is a viewBox attribute.

Can I delete it?

回答1:

Using org.w3c.dom classes you'd do something along these lines...

        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
        URL url = new URL(getCodeBase(), "fileName.svg");
        Document doc = f.createDocument(url.toString());

        Element svg = doc.getDocumentElement();

        if (svg.hasAttribute("viewBox")) {
          // notify the user somehow
        }

to delete call

        svg.removeAttribute("viewBox")