我尝试从现有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 "/>".
我也在谷歌搜索但我找不到任何解决方案。如何解决这个问题。
感谢。
答案 0 :(得分:0)
它们阻止或重定向使用默认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());
手动解析Feed非常繁琐且容易出错(您的getXMLAttributeValue
方法会抛出NullPointerExceptions)。我建议你改用https://ngmap.github.io/之类的东西。