在 Java 中解析 GML 时出现错误字符

发布于 2024-10-15 10:25:57 字数 1466 浏览 10 评论 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 "";
}

I'm using the org.w3c.dom package to parse the gml schemas (http://schemas.opengis.net/gml/3.1.0/base/).

When I parse the gmlBase.xsd schema and then save it back out, the quote characters around GeometryCollections in the BagType complex type come out converted to bad characters (See code below).

Is there something wrong with how I'm parsing or saving the xml, or is there something in the schema that is off?

Thanks,

Curtis

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 "";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2024-10-22 10:25:57

我对这一行表示怀疑:

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

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

为什么不直接使用:

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

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

I'm suspicious of this line:

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

I don't know what IOUtils.toString does here but presumably it's assuming a particular encoding, without taking account of the XML declaration.

Why not just use:

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

Likewise your FileUtils.writeStringToFile doesn't appear to specify a character encoding... which encoding does it use, and why encoding is in the StreamResult?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文