use*_*667 5 java android xml-parsing domparser
我必须开发一个Android应用程序.
这里我遵循以下xml格式.
<Product>
<product name="viki" productid="111">
<ProductType>
<producttype>Nokia</producttype>
<producttype>Samsung</producttype>
</ProductType>
</product>
</Product>
Run Code Online (Sandbox Code Playgroud)
在这里,我必须获得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);
}
Run Code Online (Sandbox Code Playgroud)
这里得到的价值是诺基亚独自.但我需要显示价值诺基亚,三星...如果我必须运行应用程序意味着获得单一的价值.但我需要得到所有值的列表..
我的代码有什么问题..请检查并给我解决方案这些???
你只获得一个<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);
}
Run Code Online (Sandbox Code Playgroud)
我直接使用了Node.getChildNodes()和Node.getTextContexnt()方法,而不是Element首先使用类型转换并使用它的方法或XMLfunctions实用程序类.
我还建议为子节点使用不同的名称,而不是依赖于使用不同的情况来避免将来出现此类问题.避免名称冲突的简单方法(当您无法提出不同的名称时)只需使用复数类似于<ProductTypes>父标记.
但是,当您需要在DOM树中深入解析时,更好的方法是使用a XPath来直接获取您感兴趣的节点列表.我不完全确定该程序的功能,只是为了给您一个示例XPath喜欢
String xpath = "//product[@name=\"viki\"]/ProductType/producttype";
Run Code Online (Sandbox Code Playgroud)
会给你NodeList的<producttype>直接节点.