如何使用Android的不同的父节点解析XML [英] How to parse xml using different parent nodes in android
问题描述
在我的XML文件,我有两个或两个以上的父节点,如: -
//父节点比萨饼 //属性是1素食披萨
//父节点汉堡
//属性是1素食汉堡,
每当用户点击比萨饼列表视图
项目,然后用户应该能够查看其他活动的列表视图的比萨项目只列出
。我知道该怎么称呼下一个活动等等,但我想知道我可以在我的Java类中使用不同的父节点,究竟是什么我需要做的,我的Java类中的更改,请参阅此链接,我使用本教程中,但在这里,他们使用的是只有一个父节点首歌,但我想添加更多,我已经加在我的XML文件,但不知道什么是我需要做的java类的变化。
carrier.xml ILE包含XML内容如下
< XML版本=1.0编码=UTF-8&GT?;
<载体>
<车辆GT;
<汽车
CID =1
CNAME =TAXI/>
< vehivle
CID =0
CNAME =AUTO/>
< /车辆GT;
//在你的code
XmlPullParser XPP = NULL;
INT EVENTTYPE = 0;
XmlPullParserFactory厂= NULL;
BufferedReader中BUFR = NULL;
StringBuilder的总=;
尝试{
BUFR =新的BufferedReader(新的FileReader(/ carrier.xml));
总=新的StringBuilder();
串线=;
而((行= bufr.readLine())!= NULL)
{
total.append(线);
}
工厂= XmlPullParserFactory.newInstance();
factory.setNamespaceAware(真正的);
XPP = factory.newPullParser();
xpp.setInput(新StringReader(新字符串(总)));
EVENTTYPE = xpp.getEventType();
}赶上(XmlPullParserException E){
// Log.v(xmlpullparser,+ E);
e.printStackTrace();
}赶上(IOException异常即){
// Log.v(IOException异常,+即);
ie.printStackTrace();
}
尝试{
而(EVENTTYPE!= XmlPullParser.END_DOCUMENT){
如果(EVENTTYPE == XmlPullParser.START_TAG){
串SCID =,Scname =;
INT acount = xpp.getAttributeCount();
如果(acount!= - 1){
为(中间体X = 0 X - 其中; acount; X ++){
如果(xpp.getAttributeName(X).equalsIgnoreCase(CID)){
SCID = xpp.getAttributeValue(X);
}如果(xpp.getAttributeName(X).equals(CNAME)){
Scname = xpp.getAttributeValue(X);
}
}
}
}
EVENTTYPE = xpp.next();
}
}赶上(例外五){
e.printStackTrace();
}
在此之后,你可以使用SCID Scname等在code按你的方便
In my xml file I have two or more parent nodes like:-
//parent node pizza //attributes are 1 Veg Pizza
//parent node burger
//attributes are 1 Veg Burger,
Whenever the user clicks on pizza listview
item then the user should be able to view only list of pizza items in another activity's listview
. I know how to call next activity and so on, but I want to know how I can use different parent nodes in my java class, exactly what are the changes I need to do in my java class, please see this link, I am using this tutorial, but here they are using only one parent node song but I want to add more and I have added in my xml file but don't know what are the changes I need to do in java class.
carrier.xml ile contains below xml contents
<?xml version="1.0" encoding="utf-8"?>
<carrier>
<vehicle>
<vehicle
cid="1"
cname="TAXI" />
<vehivle
cid="0"
cname="AUTO" />
</vehicle>
// In your code
XmlPullParser xpp=null;
int eventType = 0;
XmlPullParserFactory factory =null;
BufferedReader bufr=null;
StringBuilder total ="";
try{
bufr = new BufferedReader(new FileReader("/carrier.xml"));
total = new StringBuilder();
String line="";
while ((line = bufr.readLine()) != null)
{
total.append(line);
}
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
xpp = factory.newPullParser();
xpp.setInput(new StringReader(new String(total)));
eventType = xpp.getEventType();
}catch(XmlPullParserException e){
// Log.v("xmlpullparser"," "+e);
e.printStackTrace();
}catch(IOException ie){
// Log.v("IOException"," "+ie);
ie.printStackTrace();
}
try{
while (eventType != XmlPullParser.END_DOCUMENT){
if(eventType == XmlPullParser.START_TAG){
String Scid="",Scname="";
int acount=xpp.getAttributeCount();
if(acount!=-1){
for(int x=0;x<acount;x++){
if(xpp.getAttributeName(x).equalsIgnoreCase("cid")){
Scid=xpp.getAttributeValue(x);
}if(xpp.getAttributeName(x).equals("cname")){
Scname=xpp.getAttributeValue(x);
}
}
}
}
eventType = xpp.next();
}
}catch(Exception e){
e.printStackTrace();
}
After this you can use the Scid Scname etc in your code as per your convenience
这篇关于如何使用Android的不同的父节点解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!