我正在使用 org.w3c.dom 包来解析 gml 架构 (http://schemas.opengis.net/gml/3.1.0/base/)。
当我解析 gmlBase.xsd 架构然后将其保存回来时,BagType 复杂类型中 GeometryCollections 周围的引号字符会转换为错误字符(请参阅下面的代码)。
我解析或保存 xml 的方式是否有问题,或者架构中是否存在某些问题?
谢谢
柯蒂斯
public static void main(String[] args) throws IOException
{
File schemaFile = File.createTempFile("gml_", ".xsd");
FileUtils.writeStringToFile(schemaFile, getSchema(new URL("http://schemas.opengis.net/gml/3.1.0/base/gmlBase.xsd")));
System.out.println("wrote file: " + schemaFile.getAbsolutePath());
}
public static String getSchema(URL schemaURL)
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(IOUtils.toString(schemaURL.openStream()))));
Element rootElem = doc.getDocumentElement();
rootElem.normalize();
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
ByteArrayOutputStream xmlOutStream = new ByteArrayOutputStream();
StreamResult result = new StreamResult(xmlOutStream);
transformer.transform(source, result);
return xmlOutStream.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return "";
}
最佳答案
我对这一行表示怀疑:
Document doc = db.parse(new InputSource(
new StringReader(IOUtils.toString(schemaURL.openStream()))));
我不知道 IOUtils.toString
在这里做什么,但大概它假设了特定的编码,而不考虑 XML 声明。
为什么不直接使用:
Document doc = db.parse(schemaURL.openStream());
同样,您的 FileUtils.writeStringToFile
似乎没有指定字符编码...它使用哪种编码,以及为什么编码位于 StreamResult
中?
关于java - 在 Java 中解析 GML 时出现错误字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4855078/