Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中解析xml数据_Android_Xml_Parsing - Fatal编程技术网

在android中解析xml数据

在android中解析xml数据,android,xml,parsing,Android,Xml,Parsing,我想在android中解析以下数据 <?xml version="1.0" encoding="UTF-8"?> Info: POST /Remindz_api/user/loginHTTP/1.1 Host: www.narola.co Accept: www.narola.co.beepz.api+xml HTTP 1.1 200 OK Content-Type: www.narola.co.beepz.api+xml; Allow : GET,POST

我想在android中解析以下数据

<?xml version="1.0" encoding="UTF-8"?>

 Info: POST /Remindz_api/user/loginHTTP/1.1 
 Host: www.narola.co 
 Accept: www.narola.co.beepz.api+xml 
 HTTP 1.1 200 OK 
 Content-Type: www.narola.co.beepz.api+xml; 
 Allow : GET,POST

  <user id="43">
    <firstname>Dfdf</firstname>
    <lasttname>p2</lasttname>
    <email>p</email>
    <telephone>2236</telephone>
    <created_on>2013-01-04 04:38:05</created_on>
    <atom:link <a href="http://www.narola.co/remindz/reminders/43"></a> />
  </user>

感谢advace。

使用xml解析技术,如
XmlPullParser
SAX解析器
DOM解析器


XML Pull parser是开发者网站推荐的解析器,android是Pull parser的教程

首先,必须从收到的文本块中提取正确的XML

这取决于两种操作:

  • 剥离所有与HTTP相关的行
  • 修改

此任务可以通过先前使用正则表达式处理原始文本来执行。在您的情况下,可以使用这些表达式

public class XMLTest {

    static String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n" + " Info: POST /Remindz_api/user/loginHTTP/1.1\n"
            + " Host: www.narola.co \n" + " Accept: www.narola.co.beepz.api+xml\n" + " HTTP 1.1 200 OK \n"
            + " Content-Type: www.narola.co.beepz.api+xml;\n" + " Allow : GET,POST\n" + "\n" + "  <user id=\"43\">\n"
            + "    <firstname>Dfdf</firstname>\n" + "    <lasttname>p2</lasttname>\n" + "    <email>p</email>\n"
            + "    <telephone>2236</telephone>\n" + "    <created_on>2013-01-04 04:38:05</created_on>\n"
            + "    <atom:link <a href=\"http://www.narola.co/remindz/reminders/43\"></a> />\n" + "  </user>";

    public static void main(final String[] args) {
        /*
         * This strips off "Param:Value"-style lines 
         */
        String xmlData = data.replaceAll(" *[a-z\\-A-Z]* *:[^<]*\n", "");
        /*
         * This strips off "HTTP line" 
         */
        xmlData = xmlData.replaceAll(" *HTTP .*\n", "");

        /*
         * Correct atom:link format
         */
        xmlData = xmlData.replaceAll("<atom:link (.*) />", "<atom:link>$1</atom:link>"); 

        try {
            DocumentBuilder newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            Document doc = newDocumentBuilder.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8")));

            Element elem = doc.getDocumentElement();
            dump("", elem);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void dump(final String pad, final Node node)
    {
        System.out.println(pad + node.toString());

        if(node.getChildNodes() != null)
        {
            for(int i=0; i< node.getChildNodes().getLength();i++)
            {
                dump(pad + " ", node.getChildNodes().item(i));
            }
        }
    }
公共类XMLTest{
静态字符串数据=“\n”+”\n“+”信息:POST/REMENTZU api/user/loginHTTP/1.1\n”
+“主机:www.narola.co\n”+“接受:www.narola.co.beepz.api+xml\n”+“HTTP 1.1200确定\n”
+内容类型:www.narola.co.beepz.api+xml;\n“+”允许:获取,发布\n“+”\n“+”\n
+“Dfdf\n”+“p2\n”+“p\n”
+“2236\n”+“2013-01-04 04:38:05\n”
+“/>\n”+”;
公共静态void main(最终字符串[]args){
/*
*这将去掉“Param:Value”样式线
*/

字符串xmlData=data.replaceAll(“**[a-z\\-a-z]**:[^只需完成此操作并自行创建。我无法使用此操作分析字符串,因为我的父标记是这样的。然后尝试此操作。它确实有效。您是否尝试过执行XMLTest?生成的XML是有效的XML。解析器可能对根元素和XML声明之间的空格有点挑剔。什么完全不起作用?转储错误的堆栈跟踪我升级了代码,以便有效地处理XML,它是正确的。所有元素都被转储。@Fermando您和我都没有错。请将我的数据作为一个整体,然后应用您的语句它不起作用。它只对您的字符串起作用(由您通过大量连接开发)我不知道为什么它不起作用。
public class XMLTest {

    static String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n" + " Info: POST /Remindz_api/user/loginHTTP/1.1\n"
            + " Host: www.narola.co \n" + " Accept: www.narola.co.beepz.api+xml\n" + " HTTP 1.1 200 OK \n"
            + " Content-Type: www.narola.co.beepz.api+xml;\n" + " Allow : GET,POST\n" + "\n" + "  <user id=\"43\">\n"
            + "    <firstname>Dfdf</firstname>\n" + "    <lasttname>p2</lasttname>\n" + "    <email>p</email>\n"
            + "    <telephone>2236</telephone>\n" + "    <created_on>2013-01-04 04:38:05</created_on>\n"
            + "    <atom:link <a href=\"http://www.narola.co/remindz/reminders/43\"></a> />\n" + "  </user>";

    public static void main(final String[] args) {
        /*
         * This strips off "Param:Value"-style lines 
         */
        String xmlData = data.replaceAll(" *[a-z\\-A-Z]* *:[^<]*\n", "");
        /*
         * This strips off "HTTP line" 
         */
        xmlData = xmlData.replaceAll(" *HTTP .*\n", "");

        /*
         * Correct atom:link format
         */
        xmlData = xmlData.replaceAll("<atom:link (.*) />", "<atom:link>$1</atom:link>"); 

        try {
            DocumentBuilder newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            Document doc = newDocumentBuilder.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8")));

            Element elem = doc.getDocumentElement();
            dump("", elem);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void dump(final String pad, final Node node)
    {
        System.out.println(pad + node.toString());

        if(node.getChildNodes() != null)
        {
            for(int i=0; i< node.getChildNodes().getLength();i++)
            {
                dump(pad + " ", node.getChildNodes().item(i));
            }
        }
    }
<?xml version="1.0" encoding="UTF-8"?>

  <user id="43">
    <firstname>Dfdf</firstname>
    <lasttname>p2</lasttname>
    <email>p</email>
    <telephone>2236</telephone>
    <created_on>2013-01-04 04:38:05</created_on>
    <atom:link><a href="http://www.narola.co/remindz/reminders/43"></a></atom:link>
  </user>