0

我正在使用 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 "";
}
4

1 回答 1

1

我怀疑这条线:

Document doc = db.parse(new InputSource(
     new StringReader(IOUtils.toString(schemaURL.openStream()))));

我不知道IOUtils.toString这里做了什么,但大概它假设了一个特定的编码,而不考虑 XML 声明。

为什么不直接使用:

Document doc = db.parse(schemaURL.openStream());

同样,您FileUtils.writeStringToFile似乎没有指定字符编码...它使用哪种编码,以及为什么编码在StreamResult

于 2011-01-31T19:34:44.747 回答