android中的Xml解析
每个人我都在这样做xml解析android中的Xml解析,android,Android,每个人我都在这样做xml解析 public class XMLHandler extends DefaultHandler{ // =========================================================== // Fields // =========================================================== static ArrayList<Category1> cat_list=new
public class XMLHandler extends DefaultHandler{
// ===========================================================
// Fields
// ===========================================================
static ArrayList<Category1> cat_list=new ArrayList<Category1>();
static ArrayList<Products> product_list=new ArrayList<Products>();
Category1 cat;
Products pro;
private boolean in_outertag = false;
private boolean in_innertag = false;
private boolean in_mytag = false;
private boolean in_mytag1 = false;
private boolean in_mytag2 = false;
private boolean in_mytag3 = false;
private XMLDataSet myParsedExampleDataSet = new XMLDataSet();
// ===========================================================
// Getter & Setter
// ===========================================================
public XMLDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new XMLDataSet();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("root")) {
this.in_outertag = true;
}else if (localName.equals("Categories")) {
this.in_innertag = true;
}else if (localName.equals("Category")) {
cat =new Category1();
String attrValue = atts.getValue("id");
int i = Integer.parseInt(attrValue);
myParsedExampleDataSet.setExtractedInt(i);
cat.setCatId(i+"");
//cat_id[i]=myParsedExampleDataSet.setExtractedInt(i);
//Log.i("id", cat.setCatId(i+""));
String attrValue1 = atts.getValue("pid");
int i1 = Integer.parseInt(attrValue1);
myParsedExampleDataSet.setExtractedInt(i1);
cat.setPid(i1+"");
//p_id[i1]=myParsedExampleDataSet.setExtractedInt(i1);
//Log.i("pid", myParsedExampleDataSet.setExtractedInt(i1)+"");
this.in_mytag = true;
}else if (localName.equals("title")) {
// Extract an Attribute
this.in_mytag1 = true;
}else if (localName.equals("products")) {
this.in_innertag = true;
}else if (localName.equals("product")) {
pro=new Products();
String attrValue = atts.getValue("catid");
int i = Integer.parseInt(attrValue);
myParsedExampleDataSet.setExtractedInt(i);
pro.setCatId(i+"");
//Log.i("catid", myParsedExampleDataSet.setExtractedInt(i)+"");
this.in_mytag = true;
}else if (localName.equals("name")) {
// Extract an Attribute
this.in_mytag2 = true;
}else if (localName.equalsIgnoreCase("url")) {
// Extract an Attribute
this.in_mytag3 = true;
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("root")) {
this.in_outertag = false;
}else if (localName.equals("Categories")) {
this.in_innertag = false;
}else if (localName.equals("Category")) {
cat_list.add(cat);
this.in_mytag = false;
}else if (localName.equals("title")) {
this.in_mytag1=false;
}else if (localName.equals("products")) {
this.in_innertag=false;
}else if (localName.equals("product")) {
product_list.add(pro);
this.in_mytag=false;
}else if (localName.equals("name")) {
this.in_mytag2=false;
}else if (localName.equalsIgnoreCase("url")) {
this.in_mytag3=false;
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.in_mytag1){
myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
cat.setCatName(new String(ch, start, length));
}
if(this.in_mytag2){
myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
pro.setProductId(new String(ch, start, length));
}
if(this.in_mytag3){
String chars = new String(ch, start, length);
chars = chars.trim();
//myParsedExampleDataSet.setExtractedString(chars);
pro.setUrl(chars);
}
}
公共类XMLHandler扩展了DefaultHandler{
// ===========================================================
//田地
// ===========================================================
静态ArrayList目录=新ArrayList();
静态ArrayList产品列表=新ArrayList();
类别1猫;
产品专业;
私有布尔in_outertag=false;
_innertag中的私有布尔值=false;
_mytag中的私有布尔值=false;
_mytag1中的私有布尔值=false;
_mytag2中的私有布尔值=false;
_mytag3中的私有布尔值=false;
私有XMLDataSet myParsedExampleDataSet=新XMLDataSet();
// ===========================================================
//吸气剂和塞特
// ===========================================================
公共XMLDataSet getParsedData(){
返回此.myParsedExampleDataSet;
}
// ===========================================================
//方法
// ===========================================================
@凌驾
public void startDocument()引发异常{
this.myParsedExampleDataSet=new XMLDataSet();
}
@凌驾
public void endDocument()引发异常{
//无事可做
}
/**在开始标记上调用的获取,如:
*
*当xml类似于以下内容时,可以提供属性:
* */
@凌驾
public void startElement(字符串namespaceURI、字符串localName、,
字符串qName、属性atts)引发异常{
if(localName.equals(“根”)){
this.in_outertag=true;
}else if(localName.equals(“Categories”)){
this.in_innertag=true;
}else if(localName.equals(“Category”)){
cat=新类别1();
字符串attrValue=atts.getValue(“id”);
int i=整数.parseInt(attrValue);
myParsedExampleDataSet.setExtractedInt(i);
类别setCatId(i+);
//cat_id[i]=myParsedExampleDataSet.setExtractedInt(i);
//Log.i(“id”,cat.setCatId(i+);
字符串attrValue1=atts.getValue(“pid”);
inti1=Integer.parseInt(attrValue1);
myParsedExampleDataSet.setExtractedInt(i1);
类别设定PID(i1+);
//p_id[i1]=myParsedExampleDataSet.setExtractedInt(i1);
//Log.i(“pid”,myParsedExampleDataSet.setExtractedInt(i1)+”);
this.in_mytag=true;
}else if(localName.equals(“title”)){
//提取属性
this.in_mytag1=真;
}else if(localName.equals(“产品”)){
this.in_innertag=true;
}else if(localName.equals(“产品”)){
pro=新产品();
字符串attrValue=atts.getValue(“catid”);
int i=整数.parseInt(attrValue);
myParsedExampleDataSet.setExtractedInt(i);
pro.setCatId(i+);
//Log.i(“catid”,myParsedExampleDataSet.setExtractedInt(i)+”);
this.in_mytag=true;
}else if(localName.equals(“name”)){
//提取属性
this.in_mytag2=真;
}else if(localName.equalsIgnoreCase(“url”)){
//提取属性
this.in_mytag3=真;
}
}
/**在关闭标记上调用的控件,如:
* */
@凌驾
public void endElement(字符串namespaceURI、字符串localName、字符串qName)
抛出SAX异常{
if(localName.equals(“根”)){
this.in_outertag=false;
}else if(localName.equals(“Categories”)){
this.in_innertag=false;
}else if(localName.equals(“Category”)){
类别列表。添加(类别);
this.in_mytag=false;
}else if(localName.equals(“title”)){
这个。in_mytag1=false;
}else if(localName.equals(“产品”)){
this.in_innertag=false;
}else if(localName.equals(“产品”)){
产品列表。添加(pro);
this.in_mytag=false;
}else if(localName.equals(“name”)){
这个。in_mytag2=false;
}else if(localName.equalsIgnoreCase(“url”)){
这个。in_mytag3=false;
}
}
/**无法在以下结构上调用的获取:
*人物*/
@凌驾
公共无效字符(字符ch[],整数开始,整数长度){
如果(本文件为第1页){
setExtractedString(新字符串(ch,start,length));
cat.setCatName(新字符串(ch,start,length));
}
如果(本文件第2页){
setExtractedString(新字符串(ch,start,length));
pro.setProductId(新字符串(ch,start,length));
}
如果(本文件第3页){
字符串字符=新字符串(ch、开始、长度);
chars=chars.trim();
//myParsedExampleDataSet.setExtractedString(字符);
pro.setUrl(chars);
}
}
}
我解析所有东西都很好,但不是url
Xml文件如下所示
<?xml version="1.0" encoding="UTF-16"?>
<products>
<product catid="11">
<name>song1</name>
<url>http://news.google.co.in/news?edchanged=1&ned=en_il</url>
</product>
歌曲1
http://news.google.co.in/news?edchanged=1&内德=伊尔
我只得到了结果
请帮帮我,我哪里错了??
Thankx我建议使用android.sax进行解析,这要简单得多。您可以在此处阅读相关内容您可以尝试将
节点扭曲为CDATA标记,如下所示
<url><![CDATA[http://news.google.co.in/news?edchanged=1&ned=en_il]]></url>
纠正问题。问题可能出在您的字符方法中 对于给定的元素,此方法可能会被调用多次,您的代码假定它只被调用一次。e、 g.它可能需要“http://news.google.co.in,“/news?edchanged=1&”和“ned=en_il”
如果您将字符附加到适当的属性而不是设置它们,则可能会起作用。亲爱的,我阅读了此链接,但对这种情况没有帮助。谢谢你的建议……你有没有试过在Java中使用XPath?