0

A 试图从https://www.boardgamegeek.com/xmlapi/boardgame/13/catan解析 XML并获取 Language Dependence 的最高 numvotes 的值。

这是代码:

public class DomParserDemo {

    public static void main(String[] args) {

        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader("please paste XML from link");
                    Document doc = dbBuilder.parse(is);
            doc.getDocumentElement().normalize();
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nodeList = doc.getElementsByTagName("result") ;

            String targetValue = "";
            int maxNumVotes = 0;
            for (int i = 0; i < nodeList.getLength(); i++) {
                Element element = (Element) nodeList.item(i);
                int numVotes = Integer.parseInt(element.getAttribute("numvotes"));
                if (numVotes > maxNumVotes) {
                    maxNumVotes = numVotes;
                    targetValue = element.getAttribute("value");
                }
            }
            System.out.println("Value: " + targetValue + " NumVotes: " + maxNumVotes);

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

输出:

[Fatal Error] :1:10703: The entity name must immediately follow the '&' in the entity reference.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 10703; The entity name must immediately follow the '&' in the entity reference.
    at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:261)
    at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
    at DomParserDemo.main(DomParserDemo.java:17)
4

1 回答 1

0

如果您在浏览器中打开 URL 并搜索&,第一次点击会找到:

BGTG 115 - Spiel des Jahres, Then &amp; Now

&amp;是一个有效的实体引用。

如果继续搜索,第二次点击填充会找到:

Catan: Cities & Knights

那是无效的 XML。an&后面必须跟一个 name 和 a ;。要&在值中有 a ,必须将其转义为&amp;

简而言之,该 URL 返回的 XML 是无效的,Java XML 解析器会告诉您这一点。

于 2019-07-17T02:30:45.437 回答