【问题标题】:Parsing a SVG - java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8" standalone="no"?>解析 SVG - java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8" Standalone="no"?>
【发布时间】:2016-06-11 17:37:45
【问题描述】:

我正在尝试解析 SVG 文件,因为我想动态更改某些参数(尝试设置实时地图)。我正在使用 Spring MVC。 这是一个简单的例子,因为我需要了解它是如何工作的。

在我的控制器中

@RequestMapping(value="/")
    public String getHome(ModelMap model) throws ParserConfigurationException, SAXException, IOException{
        SVGParser parser = new SVGParser(loadSVG());
        model.addAttribute("parser", parser);

        return "home";
    }

loadSVG() 给了我图像的 xml 字符串(如果我在 &lt;img&gt; 标签中使用它就可以工作)。

private String loadSVG() throws IOException{

        Resource resource = new ClassPathResource("disegno.svg");
        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line= br.readLine()) != null) {
            stringBuilder.append(line);
        }
        br.close();

        String svgFile = stringBuilder.toString();

        return svgFile;

    }

SVGParser.class 是

public class SVGParser {

    public SVGParser(String uri) throws ParserConfigurationException, SAXException, IOException{ //costruttore
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(uri);


        String xpathExpression = "//circle/@id";
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();
        XPathExpression expression = null;
        NodeList svgPaths = null;
        try {
            expression = xpath.compile(xpathExpression);
            svgPaths = (NodeList)expression.evaluate(document, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        svgPaths.item(0).getNodeValue();

    }


}

我只是想看看结果是什么来理解一些东西,但我得到的很简单:

java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- Created with Inkscape (http://www.inkscape.org/) -->

发生了什么事?

【问题讨论】:

    标签: java xml spring svg


    【解决方案1】:

    您应该改用this method。您使用的方法需要一个字符串,该字符串实际上是文档的 URI。

    另外,不要“预读”内容,特别是因为在你的情况下你做错了(你打开阅读器而不指定编码)。只需将resource.getInputStream() 传递给上述方法即可。

    并使用 try-with-resources。即,像这样:

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    
    final Document document;
    
    try (
        final InputStream in = resource.getInputStream();
    ) {
        document = builder.parse(in);
    }
    
    // work with document
    

    【讨论】:

    • 谢谢,异常已经消失,现在我有了一个 SVGParser 的实例,接下来的工作是了解如何从中获取项目
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    相关资源
    最近更新 更多