2

i want to parse xml file i have xml like this

<parent>
  <Title>1</Title>
  <child>"Some data1"</child>
  <child>"Some data2"</child>
  <child>"Some data3"</child>
</parent>
<parent>
  <Title>2</Title>
  <child>"Some data1"</child>
  <child>"Some data2"</child>
  <child>"Some data3"</child>
 </parent>

any idea to parse xml like this(UP side)

Edit:

if the structure like this

 <parent>
  <Title>2</Title>
  <child>"Some data1"</child>
 </parent>

i use

  NodeList nodes1 = doc.getElementsByTagName("parent");                  

        for (int i = 0; i < nodes1.getLength(); i++) {

            Objectclass ciro = new   Objectclass ();
            Element e = (Element)nodes1.item(i);
            ciro.Title = functionsClass.getValue(e, "Title");
            ciro.child= functionsClass.getValue(e, "child");

            ArrayListClass.ItemList.add(ciro);

            }

here i am stroe data in a object class and then add object in array list then use data where i need.

but problem with same child name in parent how can i get these values..

 <parent>
  <Title>1</Title>
 <child>"Some data1"</child>
 <child>"Some data2"</child>
 <child>"Some data3"</child>
</parent>
<parent>
 <Title>2</Title>
 <child>"Some data1"</child>
 <child>"Some data2"</child>
 <child>"Some data3"</child>
</parent>

any tutorial for this..

  • so what issue you are getting in implementing SAX parser, or DOM Parser, please clarify your doubt, there is nothing wrong in this xml, generally xml records are formatted this way only. – jeet Jan 17 '12 at 05:53
  • If anyone can solve this : http://stackoverflow.com/questions/17421506/how-to-parse-same-tag-name-in-android-xml-dom-parsing – Altair Jul 02 '13 at 16:41

4 Answers4

1

your ciro.child should be an arrayList of Strings.

use following

nodes1 = doc.getElementsByTagName("parent");

    for (int i = 0; i < nodes1.getLength(); i++) {

        Objectclass ciro = new   Objectclass ();
        Element e = (Element)nodes1.item(i);

        NodeList list=e.items();

        for(int j=0;j<list.getLength();j++)
        {
            Node item=list.getNode(j);
            if(item.getNodeName().equals("title"))
                ciro.title=item.getNodeValue();
            else if(item.getNodeName().equals("child")
                ciro.childs.add(item.getNodeValue());

        }



        }
jeet
  • 29,001
  • 6
  • 52
  • 53
0

What is the problem? I can't understand. dom, sax, pull ... with any parser your data can be parsed. just make a model, and construct a ArrayList and iterating tags and store the data to your model.

lulumeya
  • 1,619
  • 11
  • 14
  • if you want to store the data with level of tag, ignore the parent tag, just loop and search for tags. are you need example codes? I think XmlPullParser is good at this case. but any parser can be used. – lulumeya Jan 17 '12 at 05:55
  • use getChildNodes to 'Element e' and iterate and search for data. I m not using dom parser and my answer is not detailed. but that case is not difficulty case i think. – lulumeya Jan 17 '12 at 06:04
  • http://www.cre8ive.kr/blog/2010/04/10/parsing-xml-in-android-dom-method/ may be useful. – lulumeya Jan 17 '12 at 06:06
0

take a count=0; and the time come when element is about to end just put the characters into the string array and increase the count and then after end element if same element is stating then make charte set null and again get characters and at end element you put it in array and so on.....

Yogesh
  • 1
0

use this code may this will help..

   nodes1 = doc.getElementsByTagName("parent");                  

        for (int i = 0; i < nodes1.getLength(); i++) {

            ObjectClass cgro = new ObjectClass();
            Element e = (Element)nodes1.item(i);
            cgro.Title = XMLfunctions.getValue(e, "Title");


            nodes1a = doc.getElementsByTagName("child");

            for(int j = 0; j < nodes1a.getLength(); j++ ){

                ObjectClass1 cgro1 = new ObjectClass1();

                 Element e2= (Element) nodes1a.item(j);
                 cgro1.child= XMLfunctions.getCharacterDataFromElement(e2);
                 ArrayListClass.ItemList1.add(cgro1);
            }


            ArrayListClass.ItemList2.add(cgro);

            }

and class use for this

    public class XMLfunctions {

public final static Document XMLfromString(String xml){

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is); 

    } catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

/** Returns element value
  * @param elem element (it is XML tag)
  * @return Element value otherwise empty String
  */
 public final static String getElementValue( Node elem ) {
     Node kid;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                 if( kid.getNodeType() == Node.TEXT_NODE  ){
                     return kid.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

 public static String getXML(){  
        String line = null;

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://p-xr.com/xml");

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (MalformedURLException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (IOException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        }

        return line;

}

public static int numResults(Document doc){     
    Node results = doc.getDocumentElement();
    int res = -1;

    try{
        res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
    }catch(Exception e ){
        res = -1;
    }

    return res;
}

public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return XMLfunctions.getElementValue(n.item(0));
}

public static String getCharacterDataFromElement(Element e) {
      Node child = e.getFirstChild();
      if (child instanceof CharacterData) {
         CharacterData cd = (CharacterData) child;
         return cd.getData();
      }
      return "?";
    }

   }

may this help you...

Deepak Swami
  • 3,838
  • 1
  • 31
  • 46