首页 新闻 搜索 专区 学院

android解析XML 求请教 就下面一段XML是从服务器返回的(需要解析“成功”出来)

0
悬赏园豆:140 [已解决问题] 解决于 2014-01-02 16:57

那三种解析的办法都试过了,android自带的pull  dom sax 都解析不了

<string xmlns="http://www.fillaway.com.cn">
<?xml version="1.0" encoding="utf-8"?>
<PDAExchange returnMsg="成功">
<Supplier></Supplier>
<Vid>349</Vid>
<ScanState/>
<Authority>
<Option>1</Option>
<Option>2</Option>
<Option>3</Option>
<Option>4</Option>
</Authority>
</PDAExchange>
</string>

墨者1906的主页 墨者1906 | 初学一级 | 园豆:76
提问于:2013-12-30 13:59
< >
分享
最佳答案
1

<?xml version="1.0" encoding="utf-8"?>
<PDAExchange returnMsg="成功">
<Supplier></Supplier>
<Vid>349</Vid>
<ScanState/>
<Authority>
<Option>1</Option>
<Option>2</Option>
<Option>3</Option>
<Option>4</Option>
</Authority>
</PDAExchange>

<?xml version="1.0" encoding="utf-8"?> 解析的时候这个必须在第一行

收获园豆:140
林J | 菜鸟二级 |园豆:202 | 2013-12-30 16:02

恩 我复制错了

墨者1906 | 园豆:76 (初学一级) | 2013-12-31 14:13

帮忙看看应该怎么写问题出在哪里?
服务器返回的一段XML:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.fillaway.com.cn">
<PDAExchange returnMsg="成功">
<Supplier>
</Supplier><Vid>349
</Vid><ScanState/>
<Authority>
<Option>1</Option>
<Option>2</Option>
<Option>3</Option>
<Option>4</Option>
</Authority>
</PDAExchange>
</string>

 //这个result装的东西就是上面这段XML

 System.err.println("要解析的XML是:"+result);

            InputStream  inStream= new ByteArrayInputStream(result.getBytes());
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            //    得到一个DocumentBuilder解析类
            DocumentBuilder builder = factory.newDocumentBuilder();
            //    接收一个xml的字符串来解析xml,Document代表整个xml文档
            Document document = builder.parse(inStream);
            //    得到xml文档的根元素节点
            Element personsElement = document.getDocumentElement();
            //    得到标签为PDAExchange的Node对象的集合NodeList
            NodeList nodeList = personsElement.getElementsByTagName("string");
            for(int i = 0; i < nodeList.getLength(); i++)
            {
                //    如果该Node是一个Element
                if(nodeList.item(i).getNodeType() == Document.ELEMENT_NODE)
                {
                    Element personElement = (Element)nodeList.item(i);
                    //    得到Node的属性值
                    String Nodname = personElement.getAttribute("returnMsg");
                    System.err.println(Nodname);
                    
                    NodeList childNodesList = personElement.getChildNodes();
                    for(int j = 0; j < childNodesList.getLength(); j++)
                    {
                        if(childNodesList.item(j).getNodeType() == Document.ELEMENT_NODE)
                        {
                            //    解析到了person下面的name标签
                            if("Vid".equals(childNodesList.item(j).getNodeName()))
                            {
                                //    得到name标签的文本值
                                String name = childNodesList.item(j).getFirstChild().getNodeValue();
                                System.out.println(name);
                            }
                            else if("Authority".equals(childNodesList.item(j).getNodeName()))
                            {
                                String age = childNodesList.item(j).getFirstChild().getNodeValue();
                                System.out.println(age);
                                
                            }
                        }
                    }
         }
                
            }

墨者1906 | 园豆:76 (初学一级) | 2013-12-31 14:14

@墨者1906: 

林J | 园豆:202 (菜鸟二级) | 2013-12-31 14:43

@林J:   //    得到xml文档的根元素节点 这里得到的已经是string 节点
            Element personsElement = document.getDocumentElement();
            //    得到标签为PDAExchange的Node对象的集合NodeList
            NodeList nodeList = personsElement.getElementsByTagName("PDAExchange");

林J | 园豆:202 (菜鸟二级) | 2013-12-31 14:53

@林J: 

谢谢提醒   更改代码后问题还是没解决

墨者1906 | 园豆:76 (初学一级) | 2013-12-31 17:50

@墨者1906: @林J: 

   System.err.println("要解析的XML是:"+result);
            
            sText.setText("服务器返回的XML: " + result);
            //响应的结果为XML 开始解析
            DocumentBuilderFactory factory = null;
            DocumentBuilder builder = null;
            Document document = null;
            inputStreamA = null;
            factory = DocumentBuilderFactory.newInstance();
            inputStreamA = new ByteArrayInputStream(result.getBytes());
            factory = DocumentBuilderFactory.newInstance();
            //    得到一个DocumentBuilder解析类
            builder = factory.newDocumentBuilder();
            //    接收一个xml的字符串来解析xml,Document代表整个xml文档
            document = builder.parse(inputStreamA);
           
            //    得到xml文档的根元素节点
            Element personsElement = document.getDocumentElement();
            //    得到标签为PDAExchange的Node对象的集合NodeList
            NodeList nodeList = personsElement.getElementsByTagName("PDAExchange");
            System.err.print("nodeListl集合的有:"+nodeList.getLength()+"个元素");
            for (int i = 0; i < nodeList.getLength(); i++) {
                //    如果该Node是一个Element
                if (nodeList.item(i).getNodeType() == Document.ELEMENT_NODE) {
                    Element personElement = (Element) nodeList.item(i);
                    //    得到Node的属性值
                    String Nodname = personElement.getAttribute("returnMsg");
                    System.err.print(Nodname);
 
                    NodeList childNodesList = personElement.getChildNodes();
                    for (int j = 0; j < childNodesList.getLength(); j++) {
                        if (childNodesList.item(j).getNodeType() == Document.ELEMENT_NODE) {
                            //    解析到了person下面的name标签
                            if ("Vid".equals(childNodesList.item(j).getNodeName())) {
                                //    得到name标签的文本值
                                String name = childNodesList.item(j).getFirstChild().getNodeValue();
                                System.err.print(name);
                            } else if ("Authority".equals(childNodesList.item(j).getNodeName())) {
                                String age = childNodesList.item(j).getFirstChild().getNodeValue();
                                System.err.print(age);
                            }
                           
                        }
                       
                    }
                }   
            }
            }catch(Exception e){
             e.printStackTrace();
            }finally {
             try {
     inputStreamA.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
            }

墨者1906 | 园豆:76 (初学一级) | 2013-12-31 17:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册