Question

How can i read the more attribute values on single document.

This is xml sample xml file:

<Categories>
 <Category name="Title 1"
 <SubCategory>
  <subcategory title="subtitle 1"  id="1" ></subcategory>
  <subcategory title="subtitle 2"  id="2" ></subcategory>
  <subcategory title="subtitle 3"  id="3" ></subcategory>
  </SubCategory>
   </Category>
    <Category>
     <Category name="Title 2"
 <SubCategory>
  <subcategory title="subtitle 4"  id="4" ></subcategory>
  <subcategory title="subtitle 5"  id="5" ></subcategory>
  <subcategory title="subtitle 6"  id="6" ></subcategory>
  </SubCategory>
  </Category>
   </Categories>

Here i have to display the category name and subcategory title.i have used below code and i have to run the app which means am getting the category name but i didn't get the subcategory title name..how can i get the subcategory title name ... pls give me any idea..

i have declared

 static String KEY_CATEGORY = "Categories";
    static final String KEY_TITLE = "Category";
    static final String KEY_ARTICLE = "SubCategory";

After that i have to create the nodelist and tag the category name.

 NodeList nl = doc.getElementsByTagName(KEY_TITLE);
  NodeList nl1 = doc.getElementsByTagName(KEY_ARTICLE);

Also add the all category name on map..so i have used for loop;

 for (int i = 0; i < nl.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            map.put( KEY_TITLE,(e.getAttribute("name")));
      for (int j = 0; j < nl1.getLength(); j++) {

              map.put( KEY_ARTICLE,((Element)nl1.item(i)).getAttribute("title"));

        }  
            songsList.add(map);  
        }
  }

Here am getting category name ...but i can't get the all subcategory title name...am getting only first 2 subcategory title name ...whats wrong in my code...i have used for loop within for loop..first for loop for get the category name and second for loop for getting the subcategory title...total category is 2...but subcategory is 6...thats y am getting 2 subcategory title alone i mean its depends upon total category count..if yes means how can i do for getting all subcategory title ????pls give me solution for getting the all subcategory title name...give me any idea ???

Was it helpful?

Solution

I finally figured out that you are using the android.sax package to parse your XML file. I think you need to look at the docs for the Element class. You can find a method there that will give you the subnodes (children) of the current element.

You should also look at the other classes and interfaces available in the android.sax package. In particular, the Listener interfaces will be useful in helping you traverse the parse tree of your XML file.

OTHER TIPS

I think you can do this easily with AXIOM
refer : http://ws.apache.org/axiom/userguide/userguide.html
eg :

XMLStreamReader             parser                              = XMLInputFactory.newInstance().createXMLStreamReader(new StringBufferInputStream(responseXML));
OMElement                   documentElement                     = new StAXOMBuilder(parser).getDocumentElement();

AXIOMXPath                  oTA_HotelAvailRSXPath               = new AXIOMXPath("/soap:Envelope/soap:Body/wmHotelAvailResponse/OTA_HotelAvailRS");

oTA_HotelAvailRSXPath.addNamespace("soap",      "http://schemas.xmlsoap.org/soap/envelope/");
oTA_HotelAvailRSXPath.addNamespace("xsi",       "http://www.w3.org/2001/XMLSchema-instance");
oTA_HotelAvailRSXPath.addNamespace("xsd",       "http://www.w3.org/2001/XMLSchema");

OMElement                   oTA_HotelAvailRSOmElement           = (OMElement)oTA_HotelAvailRSXPath.selectSingleNode(documentElement);
OMElement                   errorsOmElement                     = (OMElement)new AXIOMXPath("Errors").selectSingleNode(oTA_HotelAvailRSOmElement);

String strType      = (String) new AXIOMXPath("string(Text/@Type)").selectSingleNode(errorsOmElement);
String strCode      = (String) new AXIOMXPath("string(Text/@Code)").selectSingleNode(errorsOmElement);
String strDescription = (String) new AXIOMXPath("string(Text)").selectSingleNode(errorsOmElement);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top