【问题标题】:xml parsing getting the element value nullxml解析获取元素值null
【发布时间】:2012-09-10 14:56:18
【问题描述】:

我创建了一个 web 服务,看起来像:

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <todoLists>
- <todoList>
  <productId>smthig</productId> 
  <siteCode>smthing</siteCode> 
  <subDeptId>smthing</subDeptId> 
  </todoList>
  </todoLists>

我正在尝试使用下面的代码使用 DOM 解析器来解析它,我得到 nodeList.getLength() = 20 但没有得到元素的值。它显示为 null 我也尝试过 SAX 解析器,但那是也给null。网络服务有什么问题吗?请提出建议。

public class XMLParsingDOMExample extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /** Create a new layout to display the view */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        /** Create a new textview array to display the results */


        TextView category[];

        try {

            URL url = new URL(
            "http://xxxxxx:8080/webservicedemo/rest/todo");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("todoList");
            System.out.println("null 1");
            /** Assign textview array lenght by arraylist size */


            category = new TextView[nodeList.getLength()];
            System.out.println("null 122 nodeList.getLength()" +nodeList.getLength());
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);
                category[i] = new TextView(this);
                Element fstElmnt = (Element) node;
                category[i].setText("Name = "
                        + fstElmnt.getAttribute("siteCode"));


                layout.addView(category[i]);
                System.out.println("null 551");

            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        /** Set the layout view to display */
        setContentView(layout);

    }


    public class RSSAdapter extends BaseAdapter {
        Element rootElement;

        public RSSAdapter(Element rootElement) {
            this.rootElement = rootElement;
        }

        public int getCount() {
            // get the count of children of the root element
            return 0;
        }

        public Object getItem(int position) {
            // get the child identified by the position, 
            // this is the important part where you    
            // want to get the right child representing the RSS data that you want to get
            return null;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // implement your view here....
            return null;
        }
    }

}

【问题讨论】:

    标签: android xml web-services null xml-parsing


    【解决方案1】:

    在您的情况下,siteCode 不是其Node of todoList 的属性

    解决方案

        NodeList nlList = eElement.getElementsByTagName("siteCode").item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        category[i].setText(nValue.getNodeValue());
    

    【讨论】:

    • 感谢您的快速回复。
    【解决方案2】:

    把你的for循环改成这个

    for (int i = 0; i < nodeList.getLength(); i++) {
    
                    NodeList nodes = nodeList.item(i).getChildNodes();
                    category[i] = new TextView(this);
                    Element fstElmnt = (Element) node;
                    // this will return node value of 2nd node  
                    category[i].setText("Name = "
                            + fstElmnt.item(1).getNodeValue());
    
    
                    layout.addView(category[i]);
                    System.out.println("null 551");
    
                }
    

    【讨论】:

      【解决方案3】:

      迭代节点列表并获取每个子节点的值。

          NodeList nodeList = doc.getElementsByTagName("todoList");
          int length=nodeList.getLength();
          category = new TextView[length];
      
          for(int index=0;index<length;index++){
            Element element=(Element) nodeList.item(1ndex);
            NodeList childList=element.getElementsByTagName("siteCode");
            Element e=(Element) childList.item(0);
            Node n=e.getFirstChild();
            CharacterData data=(CharacterData)n;
            category[index].setText(data.getData());
           }//end for loop
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-08
        • 2012-04-14
        • 2012-11-05
        • 2012-04-03
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        • 2017-02-16
        相关资源
        最近更新 更多