Reputation: 1049
EveryOne I am doing xml parsing like This
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);
}
}
}
I parse all thing very good but not url....
The Xml file is like this
<?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>
I got the result ned=en_il
only
Please Help me, Where i am wrong?? Thankx
Upvotes: 0
Views: 202
Reputation: 40503
You may try by warping your <url>
node into CDATA tag like this
<url><![CDATA[http://news.google.co.in/news?edchanged=1&ned=en_il]]></url>
to rectify the issue.
Upvotes: 1
Reputation: 4753
The problem is likely to be in your characters method.
This method may be called several times for a given element, your code is assuming it will only be called once. e.g. It is likely called for "http://news.google.co.in", "/news?edchanged=1&" and "ned=en_il"
If you append the characters to appropriate properties instead of setting them it will probably work.
Upvotes: 0
Reputation: 15046
I would recommend using android.sax for parsing it is much easier. You can read about it here http://www.ibm.com/developerworks/opensource/library/x-android/
Upvotes: 0