내가 가지고있는 xml 파일을 파싱할수없나요?
uri로 사용해서 해보려고 했는데 안되네요...
내부리소스사용하는법 가르쳐주세요 ㅠ
그리고 제가 파싱하려는 xml 파일도 올려드릴테니. 이게 맞게 파싱하는건지 봐주세요..
부탁드려요 ㅎ
public class pullParser extends Activity {
/** Called when the activity is first created. */
TextView newsSub;
TextView newsSeq;
TextView newsDate;
ArrayList<news> news = new ArrayList<news>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
URI uri= new URI("files://ymc.test.seven/res/xml/cs11");
XmlPullParserFactory parserFactory = XmlPullParserFactory
.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
parser.setInput("uri 또는 URL"))); <<< 여기가 문젠대요.
// ///////////////////////////////////////////////////////////////////////
int parserEvent = parser.getEventType();
String tag = "ready..!";
String news_seq = null;
String news_sub = null;
String news_date = null;
int i;
for(i = 0; i <16; i++){
while (parserEvent != XmlPullParser.END_DOCUMENT) {
switch (parserEvent) {
case XmlPullParser.START_DOCUMENT:
Log.d("parserTest", "Parser Start..!");
break;
case XmlPullParser.START_TAG:
if(tag.equals("item")){
}else{
if (tag.equals("news_seq")) {
news_seq = parser.getText();
}
if (tag.equals("news_sub")) {
news_sub = parser.getText();
}
if (tag.equals("news_date")) {
news_date = parser.getText();
}
}
break;
case XmlPullParser.END_TAG:
if (tag.equals("item")) {
news save = new news(news_seq, news_sub, news_date);
news.add(save);
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
class CustomRow extends ArrayAdapter<news> {
Activity context;
public CustomRow(Activity c) {
super(c, R.layout.customcell, news);
this.context = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inf = context.getLayoutInflater();
View newsRow = inf.inflate(R.layout.customcell, null);
newsSub = (TextView) findViewById(R.id.newsSub);
newsSeq = (TextView) findViewById(R.id.newsSeq);
newsDate = (TextView) findViewById(R.id.newsDate);
newsSub.setText(news.get(position).news_sub);
newsSeq.setText(news.get(position).news_seq);
newsDate.setText(news.get(position).news_date);
return newsRow;
}
}
}
파싱하려는 xml
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8" ?>
<protocol>
<header>
<op_code>CS11</op_code>
<code/><msg/>
</header>
<body>
<item>
<news_seq>1112</news_seq>
<news_subject> 창업초보자가 지켜야할 10가지</news_subject>
<news_date> 2010-07-05</news_date>
</item>
<item>
<news_seq>1113</news_seq>
<news_subject> 창업경력자가 지켜야할 100가지</news_subject>
<news_date> 2010-06-05</news_date>
</item>
</body>
</protocol>
context.getAssets().open(path);
path는 assets 이하의 경로로 작성하시면 되요 예를 들어 a/test.txt 이런식으로요..
가져오신 후에 파서에 setInput에 스트림으로도 입력가능하거든요.. 그렇게 해결하시면 될 듯 합니다..