5

I have to develop an android application.

Here i have follows following xml format.

<Product>
<product name="viki" productid="111">
<ProductType>
<producttype>Nokia</producttype>
<producttype>Samsung</producttype>
</ProductType>
</product>
</Product>

Here i have to get the producttype for particluar product.so i have wrote the following code:

    if(subCategoryChildNode.hasChildNodes()){
            // parse 'Subcategory' childs
        NodeList productNL = subCategoryChildElmt.getElementsByTagName("product");
        if(productNL.getLength() > 0){

        ArrayList<Product> productAL = new ArrayList<Product>();
        Product productBean = null;
        for(int pCnt=0;pCnt<productNL.getLength();pCnt++){
            Node productNode = productNL.item(pCnt);

            Element productElmt = null;
            // parse 'product' tag attributes 
            if(productNode.hasAttributes()){
                productBean = new Product();
                productElmt = (Element)productNode;
                productBean.setmProductName(productElmt.getAttribute("name"));
            }
            if(productNode.hasChildNodes()){
                NodeList productTypeNL = productElmt.getElementsByTagName("ProductType");
                if(productTypeNL.getLength() > 0){
                    ArrayList<ProductType> ProductTypeAL = new ArrayList<ProductType>();
                    ProductType productTypeBean = null;
                    for(int ptCnt=0;ptCnt<productTypeNL.getLength();ptCnt++){
                    Node productTypeNode = productTypeNL.item(ptCnt);
                    Element productTypeElmt = null;
                    if(productTypeNode.hasChildNodes()){
                        productTypeBean = new ProductType();
                        productTypeElmt = (Element)productTypeNode;
                        productTypeBean.setmProductType(XMLfunctions.getValue(productTypeElmt,"producttype"));
                        System.out.println("Product Types are "+ " "+XMLfunctions.getValue(productTypeElmt,"producttype"));
                        ProductTypeAL.add(productTypeBean);
                    }
                    productBean.setmProductTypes(ProductTypeAL);
                    }
                productAL.add(productBean);
                }
            }
            subCategoryBean.setmProducts(productAL);
        }
        }
    subCategoryAL.add(subCategoryBean);
}

Here am getting the value is nokia alone.but i need to display the value nokia,samsung...if i have to run the app means getting single value.but i need to get the list of all values..

What's wrong in my code .. please check and give me solution fot these ???

4

2 回答 2

6

你只得到一个<producttype>(诺基亚)而不是完整列表的原因是因为你循环了<ProductType>节点的长度,认为你正在循环这些节点<producttype>

因此,您需要另一个内部循环来覆盖所有子产品类型节点,例如

for(int ptCnt=0; ptCnt < productTypeNL.getLength(); ptCnt++) {
    Node productTypeNode = productTypeNL.item(ptCnt);
    if(productTypeNode.hasChildNodes()){
        NodeList childProductTypeNL = productTypeNode.getChildNodes();
        System.out.print("Product Types are: ");
        for (int cptCnt=0; cptCnt < childProductTypeNL.getLength(); cptCnt++) {
            productTypeBean = new ProductType();
            productTypeBean.setmProductType (
                            childProductTypeNL.item(cptCnt).getTextContent());
            System.out.print(productTypeBean.getmProductType() + ", ");
            ProductTypeAL.add(productTypeBean);
        }
    }
    productBean.setmProductTypes(ProductTypeAL);
}

我直接使用了Node.getChildNodes()andNode.getTextContexnt()方法,而不是类型转换为Elementfirst 并使用它的方法或XMLfunctions实用程序类。

我还建议为子节点使用不同的名称,而不是依赖使用不同的大小写来避免将来出现此类问题。避免名称冲突的一种简单方法(当您无法想出不同的名称时)是简单地使用复数 like<ProductTypes>作为父标签。

但是,当您需要在 DOM 树中进行深入解析时,一种更好的方法是使用 anXPath直接获取您感兴趣的节点列表。我不完全确定程序做了什么,只是给您一个示例XPath喜欢

 String xpath = "//product[@name=\"viki\"]/ProductType/producttype";

会直接给你NodeListfor<producttype>节点。

于 2013-06-25T16:39:07.307 回答
1

我会说您的代码的问题之一(可能是其他代码)是您在 for 循环之前声明了productTypeBeanand productTypeElmt,并且由于之后不需要它,因此不需要它。

if(subCategoryChildNode.hasChildNodes()){
        // parse 'Subcategory' childs
    NodeList productNL = subCategoryChildElmt.getElementsByTagName("product");
    if(productNL.getLength() > 0){
        ArrayList<Product> productAL = new ArrayList<Product>();
        Product productBean = null;
        for(int pCnt=0;pCnt<productNL.getLength();pCnt++){
            Node productNode = productNL.item(pCnt);

            Element productElmt = null;
            // parse 'product' tag attributes 
            if(productNode.hasAttributes()){
                productBean = new Product();
                productElmt = (Element)productNode;
                productBean.setmProductName(productElmt.getAttribute("name"));
            }
            if(productNode.hasChildNodes()){
                NodeList productTypeNL = productElmt.getElementsByTagName("ProductType");
                if(productTypeNL.getLength() > 0){
                    ArrayList<ProductType> ProductTypeAL = new ArrayList<ProductType>();
                    for(int ptCnt=0;ptCnt<productTypeNL.getLength();ptCnt++){
                        Node productTypeNode = productTypeNL.item(ptCnt);
                        if(productTypeNode.hasChildNodes()){
                            ProductType productTypeBean = new ProductType();
                            Element productTypeElmt = (Element)productTypeNode;
                            productTypeBean.setmProductType(XMLfunctions.getValue(productTypeElmt,"producttype"));
                            System.out.println("Product Types are "+ " "+XMLfunctions.getValue(productTypeElmt,"producttype"));
                            ProductTypeAL.add(productTypeBean);
                        }
                        productBean.setmProductTypes(ProductTypeAL);
                    }
                    productAL.add(productBean);
                }
            }
            subCategoryBean.setmProducts(productAL);
        }
    }
    subCategoryAL.add(subCategoryBean);
}
于 2013-06-25T11:29:44.700 回答