2013-09-04 5 views
0

XML 및 Android 개발에 익숙하지 않습니다 ... 요소가 동일한 XML을 구문 분석해야하는이 문제가 발생했습니다. .동일한 이름을 가진 두 요소를 구문 분석합니다.

<tns:camera> 

<tns:congestionLocations> 
<tns:congestion>Free Flow</tns:congestion> 
<tns:direction>Eastbound</tns:direction> 
</tns:congestionLocations> 

<tns:congestionLocations> 
<tns:congestion>Free Flow</tns:congestion> 
<tns:direction>Westbound</tns:direction> 
</tns:congestionLocations> 


<tns:description>Bond St looking east</tns:description> 
<tns:direction>Eastbound</tns:direction> 
<tns:group>SH16-North-Western</tns:group> 
<tns:lat>-36.869</tns:lat> 
<tns:lon>174.746</tns:lon> 
<tns:name>SH16 1 Bond St</tns:name> 
<tns:viewUrl>http://www.trafficnz.info/camera/view/130</tns:viewUrl> 
</tns:camera> 

기본적으로, 전반적인 요소 (TNS : 카메라를) 구문 분석해야합니다 그것은 아래의 코드를 참조 설명하기 조금 어렵다과 (분명히 서로 구분) 혼잡 위치를 포함하지만, 같은 클래스 내에서 나는 목록보기에서 그것들 모두를 사용할 것이므로 ...

나는 이것을 어떻게 얻을 수 있습니까? 현재

, 나는 풀 파서를 사용하고, 클래스 객체로

PullParser 코드를 파싱하고 있습니다 :

case XmlPullParser.END_TAG: 
        if (tagname.equalsIgnoreCase(KEY_SITE)) {current Site 
         CameraSites.add(curCameraClass); 
        } else if (tagname.equalsIgnoreCase(KEY_DESCRIPTION)) { 

         curCameraClass.setDescription(curText); 
        }else if (tagname.equalsIgnoreCase(KEY_NAME)) { 
         curCameraClass.setName(curText); 
        } 

        break; 

친절 감사를!

답변

0

이 시도 ..

 NodeList nodeList = doc.getElementsByTagName("tns:camera"); 

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

        Node node = nodeList.item(i);  

        Element fstElmnt = (Element) node; 
        NodeList nameList = fstElmnt.getElementsByTagName("tns:group"); 
        Element nameElement = (Element) nameList.item(0); 
        nameList = nameElement.getChildNodes(); 

        System.out.println("tns:group : "+((Node) nameList.item(0)).getNodeValue()); 


        Element fstElmnt1 = (Element) node; 
        NodeList nameList1 = fstElmnt1.getElementsByTagName("tns:viewUrl"); 
        Element nameElement1 = (Element) nameList1.item(0); 
        nameList1 = nameElement1.getChildNodes(); 

        System.out.println("tns:viewUrl : "+ ((Node) nameList1.item(0)).getNodeValue()); 

//same as use to all tns:description,tns:direction and tns:lat etc., 


        if(node.getNodeType() == Node.ELEMENT_NODE) 
        { 
         Element e = (Element) node; 
         NodeList resultNodeList = e.getElementsByTagName("tns:congestionLocations"); 
         int resultNodeListSize = resultNodeList.getLength(); 
         for(int j = 0 ; j < resultNodeListSize ; j++) 
         { 
          Node resultNode = resultNodeList.item(j); 
          if(resultNode.getNodeType() == Node.ELEMENT_NODE) 
          { 
           Element fstElmnt2 = (Element) resultNode; 
           NodeList nameList2 = fstElmnt2.getElementsByTagName("tns:congestion"); 
           Element nameElement2 = (Element) nameList2.item(0); 
           nameList2 = nameElement2.getChildNodes(); 

           Log.v("tns:congestion", ""+((Node) nameList2.item(0)).getNodeValue()); 

           Element fstElmnt3 = (Element) resultNode; 
           NodeList nameList3 = fstElmnt3.getElementsByTagName("tns:direction"); 
           Element nameElement3 = (Element) nameList3.item(0); 
           nameList3 = nameElement3.getChildNodes(); 

           Log.v("tns:direction--", ""+((Node) nameList3.item(0)).getNodeValue()); 
          } 
         } 
        } 

       } 
+0

당신은 변화를 설명하시기 바랍니다 수 있습니다, 무슨 일이 일어나고 있는지 이해하고 싶습니다 다음 링크가 도움이 될 것입니다 희망? – ForeverLearning

+0

tns : 카메라가 주요 노드리스트이며 tns 안에 있습니다 : congestionLocations는 다른 노드리스트입니다. – Hariharan

관련 문제