如何将结果从 httpget 传递到 SAX 解析器
我想向 google API 发出请求,并将生成的 XML 传递给 SAX 解析器,这里都是代码...
首先是请求:
HttpClient hclient = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www.google.com/ig/api?weather=Cardiff");
HttpResponse hrep = hclient.execute(get);
HttpEntity httpEntity = hrep.getEntity();
然后是解析器:
SAXParserFactory saxpf = SAXParserFactory.newInstance();
SAXParser saxp = saxpf.newSAXParser();
XMLReader xr = saxp.getXMLReader();
ExHandler myHandler = new ExHandler();
xr.setContentHandler(myHandler);
xr.parse();
这是执行此操作的正确方法吗?以及如何连接这两个代码。
提前致谢
I'm want to make a request to google API and pass the resulting XML to SAX parser here are both codes...
First the request:
HttpClient hclient = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www.google.com/ig/api?weather=Cardiff");
HttpResponse hrep = hclient.execute(get);
HttpEntity httpEntity = hrep.getEntity();
Then the parser:
SAXParserFactory saxpf = SAXParserFactory.newInstance();
SAXParser saxp = saxpf.newSAXParser();
XMLReader xr = saxp.getXMLReader();
ExHandler myHandler = new ExHandler();
xr.setContentHandler(myHandler);
xr.parse();
Is this the right way to do this and how do I connect both codes.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SAXParser 对象可以接受输入流和处理程序。类似于:
getContent() 方法从 HttpRequest 返回输入流,而 XMLParser 对象只是我创建的一个类(据说),其中包含如何解析 XML 的定义。
编辑*
您确实应该阅读 SAXParser 的整个 API,它有几个重载方法:
正如我之前所说,有些方法采用 InputSource,有些方法采用 InputStream。
The SAXParser object can take in an input stream and the handler. So something like:
The getContent() method returns and input stream from the HttpRequest, and the XMLParser object is just a class I created (supposedly) that contains the definition of how to parse the XML.
EDIT*
You really should read the entire API for SAXParser, it has several overloaded methods:
Some of the methods take an InputSource, some take an InputStream, as I stated earlier.