gpt4 book ai didi

java - 在android中使用dom解析器获取值列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:27:08 25 4
gpt4 key购买 nike

我必须开发一个 android 应用程序。

这里我有以下 xml 格式。

<Product>
<product name="viki" productid="111">
<ProductType>
<producttype>Nokia</producttype>
<producttype>Samsung</producttype>
</ProductType>
</product>
</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";

会给你NodeList对于 <producttype>直接节点。

关于java - 在android中使用dom解析器获取值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17141563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com