在Android上解析XML

在Android上解析XML,第1张

概述我正在尝试在Android应用程序中使用API​​作为我们的计费系统,但我无法弄清楚如何解析它返回的XML.这是我的功能到目前为止的样子……publicvoidParseData(StringxmlData){try{//DocumentBuilderDocumentBuilderFactoryfactory=DocumentBu

我正在尝试在Android应用程序中使用API​​作为我们的计费系统,但我无法弄清楚如何解析它返回的XML.这是我的功能到目前为止的样子……

public voID ParseData(String xmlData){    try    {        // document Builder        documentBuilderFactory factory = documentBuilderFactory.newInstance();        documentBuilder db = factory.newdocumentBuilder();        // input Stream        inputSource inStream = new inputSource();        inStream.setCharacterStream(new StringReader(xmlData));        // Parse document into a NodeList        document doc = db.parse(inStream);        NodeList nodes = doc.getElementsByTagname("ticket");        // Loop NodeList and RetrIEve Element Data        for(int i = 0; i < nodes.getLength(); i++)        {            Node node = nodes.item(i);            if (node instanceof Element)            {                Element child = (Element)node;                String ID = child.getAttribute("ID");            }        }    }    catch(SAXException e)    {    }}

这是返回的XML数据的样子.我需要循环遍历每个元素并拉出每个元素,但我无法弄清楚如何使用DOM解析器.

<whmcsAPI>  <action>gettickets</action>  <result>success</result>  <totalresults>1</totalresults>  <startnumber>0</startnumber>  <numreturned>1</numreturned>  <tickets>   <ticket>    <ID>1</ID>    <tID>557168</tID>    <deptID>1</deptID>    <userID>1</userID>    <name><![cdaTA[Array]]></name>    <email></email>    <cc></cc>    <c>TmDEga5v</c>    <date>2009-08-03 23:14:32</date>    <subject><![cdaTA[Test Ticket]]></subject>    <message><![cdaTA[This is a test ticket>    ----------------------------    IP Address: xxx.xxx.xxx.xxx]]></message>    <status>Open</status>    <priority>Medium</priority>    <admin></admin>    <attachment></attachment>    <lastreply>2009-08-04 12:14:18</lastreply>    <flag>0</flag>    <service></service>   </ticket>  </tickets> </whmcsAPI>

解决方法:

是的SAX解析器是解决方案,这是让您入门的基本代码:

voID parseExampleFunction(){    SAXParserFactory spf = SAXParserFactory.newInstance();    SAXParser sp = spf.newSAXParser();    XMLReader xr = sp.getXMLReader();    file myfile = new file( //the XML file which you need to parse );    myfile.createNewfile();    fileinputStream fOut =  new fileinputStream(myfile);    BufferedinputStream bos = new BufferedinputStream( fOut );    /** Create handler to handle XML Tags ( extends DefaultHandler ) */    MessagesXMLHandler myXMLHandler = new MessagesXMLHandler(context);    xr.setContentHandler(myXMLHandler);    xr.parse(new inputSource(bos));}// the class where the parsing logic needs to defined.This preferably can be in a different .java file public class MessagesXMLHandler extends DefaultHandler{    //this function is called automatically when a start tag is encountered    @OverrIDe    public voID startElement(String uri, String localname, String qname,Attributes attributes) throws SAXException     //variable localname is the name of the tag    //this function is called autiomatically when an end tag is encountered    @OverrIDe    public voID endElement(String uri, String localname, String qname) throws SAXException {    }    //this function gets called to return the value stored betweeen the closing and opening Tags    @OverrIDe    public voID characters(char[] ch, int start, int length) throws SAXException {        //Now variable value has the value stored between the closing and opening Tags        String value=new String(ch,start,length);    }}
总结

以上是内存溢出为你收集整理的在Android上解析XML全部内容,希望文章能够帮你解决在Android上解析XML所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1099787.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存