javaxmlsvgbatik

Batik's SVGDocument.getElementsByTagNameNS returns empty NodeList despite of namespace and tagname being correctly set


I have a part of code parsing SVG document.

String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
        SVGDocument svgDocument = null;
        try {
            //svgDocument = factory.createSVGDocument(IMAGE_SVG);
            svgDocument = factory.createSVGDocument("file://", new FileInputStream(f
            ));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        NodeList gs= svgDocument.getElementsByTagNameNS("http://www.w3.org/2000/svg","g");
        NodeList pathes = svgDocument.getElementsByTagNameNS("http://www.w3.org/2000/svg", "path");

For strange reason gs and pathes bear no nodes inside. While in debugger is see that svgDocument identified elementsById inside it correctly, while elementsByTagNames and elementsByTagNamesNS are empty. How to resolve that issue? How to make elements load by tag name too?

enter image description here


Solution

  • It was some issue with file loading

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    
        String line = new String(Files.readAllBytes(Paths.get("path to file")), StandardCharsets.UTF_8);
    
        Document doc = docBuilder.parse(new InputSource(new StringReader(line)));
    

    and that document returns .getElementsByTagNameNS as wanted