【问题标题】:Attribute name "async" associated with an element type "script" must be followed by the ' = ' character与元素类型“script”关联的属性名称“async”必须后跟“=”字符
【发布时间】:2016-06-03 04:10:01
【问题描述】:

我正在尝试从现有 URL 解析 XML。

网址:http://www.sozcu.com.tr/2016/yazarlar/ugur-dundar/rss

try {
        URL url = new URL(urlAddress);
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
        InputSource ıo = new InputSource(url.openStream());
        Document document = (Document) dBuilder.parse(ıo);
        document.getDocumentElement().normalize();

        NodeList nodeList = document.getElementsByTagName("item");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            Element mainElement = (Element) node;
            String link = getXMLAttributeValue("link", mainElement);
            String title = getXMLAttributeValue("title", mainElement);
            String desc = getXMLAttributeValue("desc",mainElement);

            sozcuArticle.add(new Sozcu(link, title, desc));
        }
    } catch (ParserConfigurationException | IOException | SAXException ex) {
        System.out.println(ex.getMessage());
    }

也是我的 getXMLAttributeValue 方法

public String getXMLAttributeValue(String tag, Element element) {
        NodeList nodeElement = element.getElementsByTagName(tag);
        Element Element = (Element) nodeElement.item(0);
        return Element.getChildNodes().item(0).getNodeValue();
    }

当我运行程序时,出现异常。

[Fatal Error] :51:119: Attribute name "async" associated with an element type "script" must be followed by the ' = ' character.
Attribute name "async" associated with an element type "script" must be followed by the ' = ' character.
[Fatal Error] :5:409: Element type "n.length" must be followed by either attribute specifications, ">" or "/>".
Element type "n.length" must be followed by either attribute specifications, ">" or "/>".

我也在谷歌上搜索过,但我找不到任何解决方案。我该如何解决这个问题。

谢谢。

【问题讨论】:

  • 我发现的一个问题是您的声明:InputSource ıo 使用奇怪的字符表示 10 或其他字符。将其更改为 io 或其他内容。
  • 我能够重现您的问题。如果您更改 ex.getMessage() 的打印输出也很有帮助,它不会为 ex.printStackTrace() 提供太多信息
  • 问题与xml中的字符编码有关。我不确定字符集是什么,您可能必须使用它。如果您能找到正确的编码,您可以在 InputSource 上设置编码,例如:io.setEncoding("UTF-16");
  • 我的 StackTrace:[致命错误]:5:409:元素类型“n.length”必须后跟属性规范“>”或“/>”。元素类型“n.length”必须后跟属性规范“>”或“/>”。[致命错误]:51:119:与元素类型“脚本”关联的属性名称“异步”必须后跟'=' 字符。与元素类型“script”关联的属性名称“async”必须后跟“=”字符。
  • 当我将编码更改为 UTF-16 时。我得到另一个异常,例如 [致命错误] :1:1:prolog 中不允许内容。序言中不允许有内容。

标签: java rss rss-reader


【解决方案1】:

他们阻止或重定向使用默认 java 用户代理属性的客户端(例如:Java/1.8.0_71)。添加您自己的用户代理,它就可以正常工作:

DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
URLConnection uc = url.openConnection();
uc.setRequestProperty("User-Agent", "Karayel's rss reader");
Document document = (Document) dBuilder.parse(uc.getInputStream());

手动解析提要很繁琐且容易出错(您的getXMLAttributeValue 方法会抛出 NullPointerExceptions)。我建议你改用rometools 之类的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多