【发布时间】: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 字符串(如果我在 <img> 标签中使用它就可以工作)。
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/) -->
发生了什么事?
【问题讨论】: