Java 确定提要是Atom还是RSS
我试图确定给定的提要是基于Atom还是基于RSS 这是我的密码:Java 确定提要是Atom还是RSS,java,xml,rss,atom-feed,Java,Xml,Rss,Atom Feed,我试图确定给定的提要是基于Atom还是基于RSS 这是我的密码: public boolean isRSS(String URL) throws ParserConfigurationException, SAXException, IOException{ DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Do
public boolean isRSS(String URL) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder
.parse(URL);
return doc.getDocumentElement().getNodeName().equalsIgnoreCase() == "rss";
}
有更好的方法吗?如果改用SAX解析器会更好吗?根元素是确定提要类型的最简单方法
- RSS提要具有根元素
(请参阅)RSS
- Atom提要具有根元素
(请参阅)feed
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(URL);
在return语句中,Java字符串比较出错
将比较运算符==
与字符串一起使用时,它会比较引用而不是值(即检查两者是否完全相同)。您应该在这里使用equals()
方法。为了确保这一点,我建议使用equalsIgnoreCase()
:
提示:如果在
isRss()
方法中检查“rss”而不是“feed”(如Atom),则不必使用三元运算符。嗅探内容是一种方法。但请注意,atom使用名称空间,并且您正在创建一个不支持名称空间的解析器
public boolean isAtom(String URL) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory f = DocumentBuilderFActory.newInstance();
f.setNamespaceAware(true);
DocumentBuilder builder = f.newInstance().newDocumentBuilder();
Document doc = builder.parse(URL);
Element e = doc.getDocumentElement();
return e.getLocalName().equals("feed") &&
e.getNamespaceURI().equals("http://www.w3.org/2005/Atom");
}
还要注意,您不能使用equalsIgnorCase()进行比较,因为XML元素名称区分大小写
另一种方法是在HTTP GET请求中可用的内容类型头上作出反应。ATOM的内容类型为application/ATOM+xml
,RSS的内容类型为application/RSS+xml
。但我怀疑,并不是所有的RSS提要都可以信任设置这个标题
第三种选择是查看URL后缀,例如.atom和.rss
如果您使用Spring或JAX-RS,那么后两种方法很容易配置。您可以使用StAX解析器来避免将整个XML文档解析到内存中:
public boolean isAtom(String url) throws ParserConfigurationException, SAXException, IOException{
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new URL(url).openConnection());
xsr.nextTag(); // Advance to root element
return xsr.getLocalName().equals("feed") &&
xsr.getNamespaceURI().equals("http://www.w3.org/2005/Atom");
}
是的,我知道我不必这么做,我在很困的时候写了这个问题,很抱歉。@MahmoudHossam没问题,但是你更新了返回语句(return!(doc.getDocumentElement().getNodeName()=“feed”);)由于所描述的比较问题,也不起作用。在研究了如何规范化/创建解析不同rss提要格式的通用方法几个小时后,我也想到了这一点。回答得好。我希望你的方法是在一个完美的世界里根据我的经验,您将不得不处理大量的原始提要,忽略内容类型、后缀或XML元素的大小写等标准。这就是为什么我建议对根元素进行equalsIgnoreCase()检查,因为这几乎总是正确的。@Chris。我告诉你,世界是不完美的,饲料行业是混乱的。只要看看源代码。但是,请至少使用一个名称空间感知的XML解析器!我想我可以使用两种方法,一种检查RSS,另一种检查Atom。我将在Android应用程序中使用它,所以我不确定Android是否内置了StAX解析器,我不想添加额外的依赖项,因为我已经为每种提要类型添加了一个库。@MahmoudHossam-Android有
XmlPullParser
,这是它自己的StAX解析器版本:
public boolean isAtom(String url) throws ParserConfigurationException, SAXException, IOException{
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new URL(url).openConnection());
xsr.nextTag(); // Advance to root element
return xsr.getLocalName().equals("feed") &&
xsr.getNamespaceURI().equals("http://www.w3.org/2005/Atom");
}