简体   繁体   English

使用XMLStreamReader解析xml

[英]Parsing xml with XMLStreamReader

I want to parse an XML file. 我想解析一个XML文件。 The outcome has to be a list with the info about symbol="YHOO". 结果必须是包含有关symbol =“ YHOO”的信息的列表。

The XML file comes from yahoo using YQL. XML文件来自Yahoo,使用YQL。 An example: http://goo.gl/xB84eN 范例: http//goo.gl/xB84eN

private XMLStreamReader makeXML(URL url) throws XMLStreamException, IOException {
    InputStream stream = url.openStream();
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLStreamReader xmlr =  xmlif.createXMLStreamReader(stream);

Now I want to get some data out of the XML using SAX. 现在,我想使用SAX从XML中获取一些数据。 But I can't figure out how to get the attributes and elements using XMLStreamReader and his events described on: http://docs.oracle.com/javaee/5/api/javax/xml/stream/XMLStreamReader.html 但是我无法弄清楚如何使用XMLStreamReader及其下面所述的事件获取属性和元素: http : //docs.oracle.com/javaee/5/api/javax/xml/stream/XMLStreamReader.html

I tried: 我试过了:

private void readXML() throws XMLStreamException {
    while(XMLStream.hasNext()) {
          if(next == XMLStreamReader.START_ELEMENT){
             System.out.println(XMLStream.getName());

But how do I get for example all the data of "YHOO" with all the correct numbers? 但是,例如,如何获得所有具有正确数字的“ YHOO”数据?

Thanks in advance! 提前致谢!

The problem can solved by changing the Handler class of SAX. 通过更改SAX的Handler类可以解决此问题。 This is specific for a given XML file. 这特定于给定的XML文件。

I found it from this document helped me to solve my problem. 我从本文档中发现它可以帮助我解决问题。 It seems SAX is very specific so you have to change the Handler to your needs. SAX似乎非常具体,因此您必须根据需要更改Handler。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM