마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

xml dom parser 질문인데요;; 이거좀이상해요;

0 추천
package com.bingsudevil.classes;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.bingsudevil.dto.ItemInfoDto;

import android.util.Log;

public class ItemDataLoad extends Thread {
 
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try{
   URL url = new URL("http://www.wizcompany.com/gbid/app/itemxml/itemsinfo.xml");
   HttpURLConnection httpcon = (HttpURLConnection)url.openConnection();
   if(httpcon != null){
    httpcon.setDoInput(true);
    httpcon.setConnectTimeout(1000);
    if(httpcon.getResponseCode() == HttpURLConnection.HTTP_OK){
     InputStreamReader is = new InputStreamReader(httpcon.getInputStream(),"EUC-KR");
     BufferedReader br = new BufferedReader(is);
     
     String strxml ="";
     StringBuffer sb = new StringBuffer();
     while((strxml = br.readLine()) != null){
      sb.append(strxml);
     }
     
     xmlparse(sb.toString());
    }
   }
  }catch (Exception e) {
   // TODO: handle exception
   
  }
 }
 private void xmlparse(String strxml){
  try{
   ArrayList<ItemInfoDto> datalist;
   ItemInfoDto dto;
   
   
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder parser = factory.newDocumentBuilder();
   Document root = parser.parse(new InputSource(new StringReader(strxml)));
   NodeList node = (NodeList) root.getElementsByTagName("items");
   
   for(int i=0; i < node.getLength(); i++){
    Log.d("--", node.item(i).getChildNodes().item(1).getTextContent());
    Log.d("--", node.item(i).getChildNodes().item(3).getTextContent());
    Log.d("--", node.item(i).getChildNodes().item(5).getTextContent());
    Log.d("--", node.item(i).getChildNodes().item(7).getTextContent());
    Log.d("--", node.item(i).getChildNodes().item(9).getTextContent());
   }
   
  }catch (Exception e) {
   // TODO: handle exception
  }
 }
}

class부분이구요;;

 

<info>
 <items>
  <type>coffeebingsu</type>
  <itemname>커피빙수</itemname>
  <itemprice>15000원</itemprice>
  <itemcontent>커피랑 빙수를 섞었습니다.</itemcontent>
  <imagepath>http://www.wizcompany.com/gbid/app/images/coffeebingsu.jpg</imagepath>
 </items>
 <items>
  <type>berrybingsu</type>
  <itemname>딸기빙수</itemname>
  <itemprice>10000원</itemprice>
  <itemcontent>딸기랑 빙수를 섞었습니다.</itemcontent>
  <imagepath>http://www.wizcompany.com/gbid/app/images/berrybingsu.jpg</imagepath>
 </items>
</info>

xml 인데요;;

Log.d("--", node.item(i).getChildNodes().item(1).getTextContent());
    Log.d("--", node.item(i).getChildNodes().item(3).getTextContent());
    Log.d("--", node.item(i).getChildNodes().item(5).getTextContent());
    Log.d("--", node.item(i).getChildNodes().item(7).getTextContent());
    Log.d("--", node.item(i).getChildNodes().item(9).getTextContent());

 

이 부분에서요 ; 제 생각대로라면;;

node.item(i).getChildNodes().item(0)

node.item(i).getChildNodes().item(1)

node.item(i).getChildNodes().item(2)

node.item(i).getChildNodes().item(3)

node.item(i).getChildNodes().item(4)

node.item(i).getChildNodes().item(5)

 

이렇게 해야;; 값이나오는게 정상아닌가요??

제가 뭘잘못햇는지;;;; 홀수 2개씩 하니까 값이 잘나오네요 --;;;;

이거 제가 뭘모르는게 잇는건가요?? 고수님들 도와주세요 ㅠㅠ..;;

 

 

mamongs (4,050 포인트) 님이 2013년 3월 25일 질문
getChildNodes() 를 호출하면 모든 노드 (텍스트 노드 포함)이 나옵니다.
각 노드마다 엘리먼트 노드인지 검사하면서 처리해야 합니다.

1개의 답변

0 추천
xml에서 white space 제거하거나

파서에서 white space 무시하는 옵션 줘보세요.

 

dom parser는 속도도 느리고 기능도 별로여서 잘 안쓰죠.

안드로이드에서는  XmlPullParser 가 있으니 사용해보세요.
원조안드로이드 (58,190 포인트) 님이 2013년 3월 25일 답변
xmlpullparser 사용햇다가.. 제가 원하는 형식이 나오지 않아서..
dom parser로 변경한것인데요.. 이 사이트를 보면서 혼자 독학중인데.. 원조 안드로이드님 답변 정말 감사합니다 ^^;
xmlpullparser는.. node가 하나씩 루프돌면서 값이 하나씩 하나씩만 받아오는 듯하더라구요.. dto에 한꺼번에 값을 넣어서 arraylist에 넣어줘야 하는 지라.. 방법을 잘모르겟더군요 ㅠㅠ
case XmlPullParser.START_DOCUMENT:
case XmlPullParser.END_DOCUMENT:
case XmlPullParser.START_TAG:
case XmlPullParser.TEXT:
case XmlPullParser.END_TAG:

이거말구 저기 xml을 적용해서 보면..
items 노드가 2개이니 0번째 items노드가 다 돌구 나서
들어가는 조건문이나 그런 속성 혹시아시나요?ㅜㅜ
댓글을 너무 길게달앗군요 ㅡㅡ;보시려나;
...