2012-01-17 13 views
2

私はこのアンドロイドで同じ要素名のAndroid xml解析?

<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> 

この(UP側)のようなXMLを解析するための任意のアイデアのようなXMLを持っているxmlファイルを解析する

編集:

この

ような構造であれば
<parent> 
    <Title>2</Title> 
    <child>"Some data1"</child> 
</parent> 

i

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

      } 

ここで私はオブジェクトクラスのデータをストリーミングして、配列リストにオブジェクトを追加してから、必要な場所でデータを使用します。

が、私は、これらの値を得ることができますどのように親で同じ子の名前に問題

..

<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> 

このため任意のチュートリアル..あなたは、このリンクを見て、SAXParserのを使用する必要が

+0

SAXパーサ、またはDOMパーサの実装に入ると、疑問を明確にしてください。このXMLには何も問題はありません。通常、XMLレコードはこの形式になっています方法だけ。 – jeet

+0

誰もがこの問題を解決することができる場合: http://stackoverflow.com/questions/17421506/how-to-parse-same-tag-name-in-android-xml-dom-parsing – Altair

答えて

0

使用。これ

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 "?"; 
    } 

    } 

ため

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

      } 

とクラス使用することができ、このヘルプあなた...あなたが発行するどのよう

+0

ありがとうございます.. –

0

何が問題ですか?理解できません。 dom、sax、pull ...任意のパーサーでデータを解析できます。モデルを作成し、ArrayListを構築し、タグを繰り返してモデルにデータを格納します。

+0

保存したい場合はタグのレベルのデータは、親タグを無視し、ループしてタグを検索してください。あなたはサンプルコードが必要ですか?私はこの場合XmlPullParserが良いと思います。任意のパーサーを使用することができます。 – lulumeya

+0

私は –

+0

を説明するために私の質問を更新し、getChildNodesを 'Element e'にしてデータを繰り返し検索します。私はドンパーサーを使用していないと私の答えは詳細ではありません。しかし、そのケースは難しいことではないと思います。 – lulumeya

0

はカウント= 0をとります。 要素が終了しようとしているときに文字列を配列に入れてカウントを増やした後、同じ要素が現れている場合は終了要素の後にcharteをnullに設定して再び文字を取得し、最後に要素を入れます配列など.....

+0

はもっと理解しやすいように説明できます –

1

あなたのciro.childは文字列の配列リストでなければなりません。

nodes1 = doc.getElementsByTagName( "親")以下

使用。このコードは、これが役立つかもしれ

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

     } 



     } 
+0

NodeList list = e.items(); はエラーを返します。 –

関連する問題