簡體   English   中英

在android中使用dom解析器獲取值列表

[英]Get the list of values using dom parser in android

我必須開發一個Android應用程序。

這里我遵循以下xml格式。

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

在這里,我必須獲得particluar product的產品類型。所以我寫了以下代碼:

    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);
}

這里得到的價值是諾基亞獨自。但我需要顯示價值諾基亞,三星...如果我必須運行應用程序意味着獲得單一的價值。但我需要得到所有值的列表..

我的代碼有什么問題..請檢查並給我解決方案這些???

您只獲得一個<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()Node.getTextContexnt()方法,而不是首先使用類型轉換為Element並使用其方法或XMLfunctions實用程序類。

我還建議為子節點使用不同的名稱,而不是依賴於使用不同的情況來避免將來出現此類問題。 避免名稱沖突的簡單方法(當您無法提出不同的名稱時)只需使用復數(如<ProductTypes>作為父標記。

但是,當您需要在DOM樹中深入解析時,更好的方法是使用XPath直接獲取您感興趣的節點列表。我不完全確定該程序的功能,只是為了給您一個示例像XPath一樣

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

會直接為您提供<producttype>節點的NodeList

我說你的代碼問題(可能是其他代碼)之一就是你在for循環之前聲明了你的productTypeBeanproductTypeElmt ,並且由於之后不需要它,所以不需要它。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM