java - XML 中的空白

标签 java xml xpath

我正在尝试使用 SBA api 中的 xml 文件。

http://api.sba.gov/loans_grants/federal_and_state_financing_for/ny.xml

问题是当我尝试使用 xpath 解析此 xml 时,出现此错误:

[Fatal Error] loans_grants.dtd:3:22: White space is required before the attribute type in the declaration of attribute "CDATA" for element "count". Exception in thread "main" org.xml.sax.SAXParseException: White space is required before the attribute type in the declaration of attribute "CDATA" for element "count".

观察 xml 文件后,我认为问题出在以下几行和之后的类似行中:

<grant_loans count="103">

<industry nil="true"/>

<state_name nil="true"/>

我想 count 之间是否有空格和"103"nil"true"那么这个错误就不会发生。由于整个 xml 太大,我复制了其中的一部分并进行了这些更改并保存在我的本地存储中。然后我就可以运行并解析它而不会出现错误。我只是放了一些空格,如下所示:

<grant_loans count = "103">

如何使用我的程序对所有需要空间的地方执行此操作,然后使用它进行进一步解析?

如果您需要,我可以在这里发布我的 java 代码,但是该代码适用于其他 xml 文件,所以我认为这个 xml 文件有问题。

编辑

Java代码段:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder;
    Document doc = null;
    XPathExpression expr = null;
    builder = factory.newDocumentBuilder();
    doc = (Document) builder
            .parse("http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway&sensor=false");

    // Create a XPathFactory
    XPathFactory xFactory = XPathFactory.newInstance();

    // Create a XPath object
    XPath xpath = xFactory.newXPath();

    // Compile the XPath expression
    expr = xpath.compile("//geometry/location/lat/text()");
    System.out.println("expr" + expr);
    // Run the query and get a nodeset
    Object result = expr.evaluate(doc, XPathConstants.NODESET);

    // Cast the result to a DOM NodeList
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());  
    }

                       //this works
// 
// some other code
//
builder = factory.newDocumentBuilder();
    url = "http://api.sba.gov/loans_grants/federal_and_state_financing_for/ny.xml";
    doc = builder.parse(url); // problem occurs here
    xFactory = XPathFactory.newInstance();

    // Create a XPath object
    xpath = xFactory.newXPath();

    // Compile the XPath expression
    expr = xpath.compile("//grant_loan/url/text()");
    result = expr.evaluate(doc, XPathConstants.NODESET);

    // Cast the result to a DOM NodeList
    nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }

//other stuffs

最佳答案

这不是 XML。它告诉你the DTD被顶起来了。请注意错误开头的 loans_grants.dtd:3:22。它指向第 3 行:

<!ATTLIST count CDATA>

可能应该改为读取

<!ATTLIST grant_loans count CDATA #REQUIRED>

错误指出 proper format ATTLIST 的值为:

<!ATTLIST element-name attribute-name attribute-type default-value>

它在第三个位置看到字符串“CDATA”,假设这是属性名称,并且仍然期望获得属性类型,但相反,它找到了 ATTLIST 。这就是为什么它给出了关于期待空白的潜在令人困惑的信息。

最有可能的是,当您复制一些 xml 以在本地运行时,您省略了 DTD 声明,这也可以解决问题。

关于java - XML 中的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866004/

相关文章:

java - 为什么默认禁用 hibernate batching/order_inserts/order_updates?

Java GUI - JTextArea 扩展但不收缩

java - 发牌算法

jquery - MVC Razor 以 XML 形式提交数据

xml - 使用 XSD 验证 XML 中的自定义日期和时间

xml - XPath 获取没有父节点的所有子节点(元素、注释和文本)

html - 如何在带有 xpath 的名为 nav-submit-button 的网页中找到一个元素 div?

java - 为什么需要强制转换为 (LayoutManager) 来设置布局?

xml - 如何屏蔽 xml 文件中的敏感数据?

xml - 如何在xslt中检查当前节点的父节点是否为根节点?