如何使用org.xml.sax.SAXParseException处理?

时间:2019-07-17 01:29:52

标签: java xml jdom parsexml

A尝试从https://www.boardgamegeek.com/xmlapi/boardgame/13/catan解析XML,并获得最高语言依赖度的值。

这是代码:

<button data-pay-btn class="mdc-button">Pay &amp; Checkout Now</button>

输出:

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();
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

如果您在浏览器中打开URL并搜索&,则第一个匹配项将找到:

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

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

如果您继续搜索,则第二次点击填充查找:

Catan: Cities & Knights

那是无效的XML。 &之后必须是名称和;。要在值中包含&,必须将其转义为&amp;

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