在多个标签中包含标签的XML

时间:2018-06-26 11:22:17

标签: java android 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>

在textview中显示属性名称,并且如果attributebname仅是彩色的,则在Spinner中显示那个attributevalue,但是我的问题是在所有spinner中都显示了所有属性值。我的编码在下面...

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

1 个答案:

答案 0 :(得分:-1)

不要自己创建XML解析器。

使用JDOM2。或者,如果您想让生活变得困难,可以使用任何其他现有的XML解析器。