首页 > 解决方案 > XML 在多个标签中具有标签

问题描述

我正在解析 xml,但我无法解析其中的 xml 标签和另一个标签。这是 XML 结构。

 <value>
<records>
  <product>
      <product_id>30</product_id>
      <product_name>Cotton Selvets</product_name>
      <product_imagepath>1529658805.jpg</product_imagepath>
      <product_description/>
      <product_basicprice/>
      <category_id>7</category_id>
      <product_status>Active</product_status>
      <product_lastupdate>2018-06-22</product_lastupdate>
  </product>
  <attributename>
        <product_id>30</product_id>
         <attribute_name>Colour</attribute_name>
         <attribute_id>8</attribute_id>
         <attributevalue>
              <product_id>30</product_id>
              <product_retaillerprice/>
              <attribute_id>8</attribute_id>
              <attributevalue_id>38</attributevalue_id>
               <attribute_value>Blue</attribute_value>
          </attributevalue>
   </attributename>
   <attributename>
           <product_id>30</product_id>
           <attribute_name>Size</attribute_name>
           <attribute_id>1</attribute_id>
           <attributevalue>
                <product_id>30</product_id>
                <product_retaillerprice/>
                <attribute_id>1</attribute_id>
                <attributevalue_id>1</attributevalue_id>
                <attribute_value>4x6</attribute_value>
           </attributevalue>
           <attributevalue>
                 <product_id>30</product_id>
                 <product_retaillerprice/>
                 <attribute_id>1</attribute_id>
                 <attributevalue_id>16</attributevalue_id>
                 <attribute_value>Basket</attribute_value>
           </attributevalue>
    </attributename>
</records>
</value>

属性名显示在文本视图中,如果属性名是颜色,则属性值仅显示在微调器中,但我的问题是所有属性值都显示在所有微调器中。我的编码如下...

 try {
                if (response.length() > 0) {
                   /* int code = Integer.parseInt(XMLManualParser.getTagValue(ConstantUrl.TAG_CODE, response));
                    String msg = XMLManualParser.getTagValue("message", response);
                    if (code == 1) {*/
                    ProductList.clear();
                    ArrayList<String> prodcuts = XMLManualParser.getMultipleTagList("product", response);
                    for (int i = 0; i < prodcuts.size(); ++i) {
                        String responseContent = prodcuts.get(i);
                        Attribute attribute = new Attribute();
                        attribute.setProductname(XMLManualParser.getTagValue("product_name", responseContent));
                        attribute.setProductimagepath(XMLManualParser.getTagValue("product_imagepath", responseContent));
                        attribute.setProductdesc1(XMLManualParser.getTagValue("product_description", responseContent));
                        attribute.setProductbasicprize(XMLManualParser.getTagValue("product_basicprice", responseContent));
                        attribute.setProduct_id(Integer.parseInt(XMLManualParser.getTagValue("product_id", responseContent)));
                        ProductList.add(attribute);
                    }
                    attributenamearraylist.clear();
                    ArrayList<String> attributenamelist = XMLManualParser.getMultipleTagList("attributename", response);
                    for (int i = 0; i < attributenamelist.size(); ++i) {
                        String responseContent = attributenamelist.get(i);
                        attribute = new Attribute();
                        attribute.setAttr_name(XMLManualParser.getTagValue("attribute_name", responseContent));
                        attribute.setAttrebute_id(XMLManualParser.getTagValue("attribute_id", responseContent));
                        attributevaluearraylist.clear();
                        spinnerlist.clear();
                        ArrayList<String> attributevaluelist = XMLManualParser.getMultipleTagList("attributevalue", responseContent);
                        for (int j = 0; j < attributevaluelist.size(); ++j) {
                            String responsecontent = attributevaluelist.get(j);
                            Attribute attr = new Attribute();
                            attr.setAttr_value(XMLManualParser.getTagValue("attribute_value", responsecontent));
                            attr.setAttrebute_id(XMLManualParser.getTagValue("attribute_id", responsecontent));
                            attr.setDealer_prize(XMLManualParser.getTagValue("product_dealerprice", responsecontent));
                            attr.setRetailer_prize(XMLManualParser.getTagValue("product_retaillerprice", responsecontent));
                            attributevaluearraylist.add(attr);
                        }
                        attributenamearraylist.add(attribute);
                        Log.d("arraylistsize", attributevaluearraylist.size() + "");
                    }

                   /* } else {
                        Toast.makeText(ProductDescriptionActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }*/
                    attribute_adapter.notifyDataSetChanged();
                }
            } catch (
                    Exception e)

            {

            }
            pdLoading.dismiss();

标签: javaandroidxml

解决方案


不要自己制作 XML 解析器。

使用 JDOM 2。或者任何其他现有的 XML 解析器,如果你想让你的生活变得困难的话。


推荐阅读