[英]How to Parse XML in android and store it in class
<layout>
<menu1>
<title>Oil Painting</title>
<sectioncount>5</sectioncount>
<cost>0</cost>
<appcode>0</appcode>
<section1.1>
<title>Oil Painting basics</title>
<subtitle>9 minutes 57 secs*!How to do Oil Painting</subtitle>
<video1.1></video1.1>
<imgvideo>Landscape19.jpg</imgvideo>
<description>video1.html</description>
</section1.1>
<section1.2>
<title>Advacned Oil Painintg</title>
<subtitle>6 minutes 23 secs*!Advanced Oil Painting</subtitle>
<video1.2></video1.2>
<imgvideo>Landscape19.jpg</imgvideo>
<description>video1.html</description>
</section1.2>
<section1.3>
<title>Watercolors basic</title>
<subtitle>9 minutes 51 secs*!How to use watercolors</subtitle>
<video1.3></video1.3>
<imgvideo>fifa.jpg</imgvideo>
<description>video1.html</description>
</section1.3>
<section1.4>
<title>Watercolors Advanced</title>
<subtitle>3 minutes 46 secs*!Advacned training- Watercolors</subtitle>
<video1.4></video1.4>
<imgvideo>fifa.jpg</imgvideo>
<description>video1.html</description>
</section1.4>
<section1.5>
<title>Enamel Painting Basic</title>
<subtitle>1 minute 56 secs*!Advacned training- Watercolors</subtitle>
<video1.5></video1.5>
<imgvideo>fifa.jpg</imgvideo>
<description>video1.html</description>
</section1.5>
</menu1>
<menu2>
<title>Graphite Pencil Sketching</title>
<sectioncount>4</sectioncount>
<cost>.99</cost>
<appcode>0</appcode>
<section2.1>
<title>Basic Graphite Pencil Sketching</title>
<subtitle>5 minutes 29 secs*!What you need to know</subtitle>
<video2.1></video2.1>
<imgvideo>fifa.jpg</imgvideo>
<description>video1.html</description>
</section2.1>
<section2.2>
<title>Advanced Graphite Pencil Sketching</title>
<subtitle>3 minutes*!The proffessional way</subtitle>
<video2.2></video2.2>
<imgvideo>fifa.jpg</imgvideo>
<description>video1.html</description>
</section2.2>
<section2.3>
<title>Graphite Pencil Tools</title>
<subtitle>5 minutes*!what should you use</subtitle>
<video2.3></video2.3>
<imgvideo>Landscape19.jpg</imgvideo>
<description>video1.html</description>
</section2.3>
<section2.4>
<title>Do's and Don't of Graphite Pencil Sketching</title>
<subtitle>5 minutes*Cautions and precautions</subtitle>
<video2.4></video2.4>
<imgvideo>Landscape19.jpg</imgvideo>
<description>video1.html</description>
</section2.4>
</menu2>
<menu3>
<title>Pen Sketching</title>
<sectioncount>3</sectioncount>
<cost>0</cost>
<section3.1>
<title>Basic Pen Sketching</title>
<subtitle>2 minutes*!what you need to know</subtitle>
<video3.1></video3.1>
<imgvideo>fifa.jpg</imgvideo>
<description>video1.html</description>
</section3.1>
<section3.2>
<title>Advanced Pen Sketching</title>
<subtitle>3 minutes*!proffessional approach</subtitle>
<video3.2></video3.2>
<imgvideo>fifa.jpg</imgvideo>
<description>video1.html</description>
</section3.2>
<section3.3>
<title>Pen Sketching Tools</title>
<subtitle>3 minutes*!which pens to use</subtitle>
<video3.3></video3.3>
<imgvideo>fifa.jpg</imgvideo>
<description>video1.html</description>
</section3.3>
</menu3>
</layout>
--
public class twitterFeedHandler extends DefaultHandler
StringBuilder sb = null;
String ret = "";
boolean bStore = false;
int howMany = 0;
Context context;
String a;
boolean config = false;
boolean menu = false;
Boolean section = false;
String elementValue = null;
Boolean elementOn = false;
private final static String TAG_Layout = "layout";
private final static String TAG_COnfig = "configuration";
private final static String TAG_Menu = "menu";
public static XMLGettersSetters data = null;
public static XMLGettersSettersMenu datamenu = null;
twitterFeedHandler() {
}
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
try {
elementOn = true;
if (localName.equals(TAG_Layout)) {
data = new XMLGettersSetters();
datamenu = new XMLGettersSettersMenu();
}if (localName.equals(TAG_COnfig)) {
config = true;
Log.i("Config localName", "configuration start");
} else if (localName.equals(TAG_Menu)) {
Log.i("Menu localName", "menu1 start");
if (localName.equals("section1")) {
if(localName.equals("stitle1"))
Log.i("Section localName", "section start");
section = true;
}
menu = true;
Log.i("DATA --- > Menu ", datamenu.getCompany().toString());
}
} catch (Exception ee) {
Log.d("error in startElement", ee.getStackTrace().toString());
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("configuration")) {
Log.i("Config localName", "configuration end");
bStore = false;
} else if (localName.equals("menu")) {
Log.i("Menu localName", "menu end");
if (localName.equals("section1")) {
Log.i("Section localName", "section end");
section = false;
}
menu = false;
}
if(localName.equals("title"))
datamenu.setTitle(elementValue);
if(localName.equals("sectioncount"))
datamenu.setCompany_SectionCount(elementValue);
if(localName.equals("section1"))
if(localName.equals("stitle1"))
datamenu.setSection(elementValue);
Log.i("Menu localName214213132132132"," " + datamenu.getCompany_SectionCount() + " size " + datamenu.getSection());
elementOn = false;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (elementOn) {
elementValue = new String(ch, start, length);
Log.i("--------->Name", section + " " );
if (config && menu == false)
data.setCompany(elementValue);
else
datamenu.setCompany(elementValue);
elementOn = false;
}
}
public class XMLGettersSetters {
private ArrayList<String> company = new ArrayList<String>();
public ArrayList<String> getCompany() {
return company;
}
public void setCompany(String company) {
this.company.add(company);
}
}
public class XMLGettersSettersMenu {
private ArrayList<String> company = new ArrayList<String>();
private ArrayList<String> title = new ArrayList<String>();
private ArrayList<String> section_count = new ArrayList<String>();
private ArrayList<String> section = new ArrayList<String>();
public ArrayList<String> getCompany() {
return company;
}
public void setCompany(String company) {
this.company.add(company);
}
public void setTitle(String company) {
this.title.add(company);
}
public ArrayList<String> getTitle() {
return title;
}
public void setCompany_SectionCount(String company) {
this.section_count.add(company);
}
public ArrayList<String> getCompany_SectionCount() {
return section_count;
}
public void setSection(String company) {
this.section.add(company);
}
public ArrayList<String> getSection() {
return section;
}
}
}
使用SAX解析器; 這是最好的選擇。
我想解析此xml代碼
=>實現SAX解析器
我可以獲取所有值,但是在將其存儲在類中以及如何檢索它並在listview中顯示時遇到問題。我可以獲取標題,部分代碼。 但是問題是如何獲取多個節的子節點並將其存儲在ArrayList中。
對於這個問題,我建議您創建一個具有標題,副標題和其他屬性的類,並為其創建Getter / setter方法。
我為什么建議這種解決方案? 因為您嘗試創建其他ArrayList:
private ArrayList<String> company = new ArrayList<String>();
private ArrayList<String> title = new ArrayList<String>();
private ArrayList<String> section_count = new ArrayList<String>();
private ArrayList<String> section = new ArrayList<String>();
相反,您可以創建單個ArrayList並每次添加對象。
在解析整個XML的最后,您將擁有包含多個對象的ArrayList,現在您可以使用此ArrayList創建ListView的適配器。
我用這段代碼解析xml
xml is like this
<point>
<ImageFile name="hi"/>
</point>
NodeList image = doc.getElementsByTagName("Point");
Node singleTerminalNode = image.item(i);
Element firstLevel = (Element)singleTerminalNode;
NodeList value1Nodes = (firstLevel).getElementsByTagName("ImageFile");
imagess = ((Element)value1Nodes.item(imageindex)).getAttribute("name");
System.out.println("image index issssssssss " + imageindex);
希望這個能對您有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.