簡體   English   中英

XML中的空白

[英]White Space in XML

我正在嘗試使用SBA API中的xml文件。

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

問題是,當我嘗試使用xpath解析此xml時,出現以下錯誤:

[致命錯誤] loan_grants.dtd:3:22:元素“ count”的屬性“ CDATA”聲明中的屬性類型之前需要空格。 線程“主”中的異常org.xml.sax.SAXParseException:元素“ count”的屬性“ CDATA”的聲明中的屬性類型之前需要空格。

觀察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。 告訴您DTD升高了。 請注意錯誤開始處的loans_grants.dtd:3:22 它指向第3行:

<!ATTLIST count CDATA>

可能應該改為閱讀

<!ATTLIST grant_loans count CDATA #REQUIRED>

錯誤指出ATTLIST正確格式為:

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

它看到字符串“ CDATA”在第三位置,假定它是attribute-name,並且仍然希望獲得attribute-type,但是,卻找到了ATTLIST的末尾。 這就是為什么它給出了有關期望空白的潛在混亂信息。

最有可能的是,當您復制一些xml以在本地運行時,您將DTD聲明置為空,這也可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM