I am having problem in parsing the xml data, below is my code
URL url=new URL(Urls.statusUrl);
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
FOIA_Parsing_Handler mxmlparsing=new FOIA_Parsing_Handler();
xr.setContentHandler(mxmlparsing);
xr.parse(new InputSource(url.openStream()));//Exception at this line
mStatus=mxmlparsing.getRecords();
m_adapter=new StatusAdapter(Status_Inquiry.this, R.layout.status_inquiry_view, mStatus);
mLstViewStatusInquiry.setAdapter(m_adapter);
I am getting
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 8299: not well-formed (invalid token)
at above line
Regards
Saurabh
As suggested by st0le the problem is in xml data I am getting some garbage in xml so the code is coorect
Related
I am learning android and wanted to perform xml parsing using SAXParser. my issue is when i run my code i get the following error:
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 5: not well-formed (invalid token)
i have gone through similar cases reported on stackoverflow.com but none of the solutions provided have fixed my issue
here is my code:
public List<RssItem> getItems() throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
//Creates a new RssHandler which will do all the parsing.
RssHandler handler = new RssHandler();
InputSource source = new InputSource(new StringReader(rssUrl));
source.setEncoding("UTF-8");
saxParser.parse(source, handler);
return handler.getRssItemList();
the xml feed iam trying to parse is: http://t.arabi21.com/rss
Kindly help me to overcome this issue.
thanks a lot
You simply need to add XML declaration like
<?xml version='1.0' encoding='UTF-8'?>
in the beginning, before your
<rss version="2.0">
element.
I am parsing XML from URL using SAXParser. The XML has some data with ampersand (&) sign. XML data is not read after the ampersand. How would I resolve this issue?
URL website = new URL(FullURL);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingXMLStuff doingwork = new HandlingXMLStuff();
xr.setContentHandler(doingwork);
xr.parse(new InputSource(website.openStream()));
String information = doingwork.getInformation();
XML tag has data like
<choice>Cat & Dog</choice>
I am getting output as
Cat
To have a naked '&' rather than "&" you need to use a CDATA[[]] structure around the "Cat & Dog".
hi everyone I try to read the information of this direction
http://finance.yahoo.com/d/quotes.csv?s=XOM+BBDb.TO+JNJ+MSFT&f=snd1l1yr
SAXParserFactory factory= SAXParserFactory.newInstance();
try {
SAXParser parser= factory.newSAXParser();
RssHandler handler= new RssHandler();
parser.parse(this.getInputStream(), handler);
return handler.getQuotes();
} catch (Exception e) {
throw new RuntimeException(e);
}
but I have this error
not well-formed (invalid token)
if I put this direction http://finance.yahoo.com/rss/headline?s=yhoo the application works well, but I need read the other direction.
any idea!!!
thanks in advance.
The first URL contains something like this:
"XOM","Exxon Mobil Corpo","6/1/2011",82.03,2.14,11.89
"BBD-B.TO","BOMBARDIER INC., ","6/1/2011",6.95,1.11,16.07
"JNJ","Johnson & Johnson","6/1/2011",66.48,3.25,15.26
"MSFT","Microsoft Corpora","6/1/2011",24.43,2.44,9.94
which is CSV (comma-separated values) formatted data.
CSV is not a valid RSS feed format. It's not even XML.
I am trying to parse my xml file resource with SaxParser. I have created my DataHandler but I don't know how indicate to XmlReader the location of data.xml that is in res/xml/.
What is the correct parameter for InputSource object?
XmlResourceParser parser = getResources().getXml(R.xml.data);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
// Create handler to handle XML Tags ( extends DefaultHandler )
DataSaxHandler myXMLHandler = new DataSaxHandler();
xr.setContentHandler(myXMLHandler);
//R.xml.data is my xml file
InputSource is=new InputSource(getResources().getXml(R.xml.data)); //getResources... is wrong say Eclipse
xr.parse(is);
Thanks a lot.
The problem is that the call to getResources().getXml(int id) is returning a XmlResourceParser, and there is no InputSource constructor that takes an XmlResourceParser.
If you want to stick with the SaxParser, you'll need to open up an InputStream via Resources#openRawResource(int id), and then pass that to the InputSource constructor. You'll also need to move the file to res/raw to use the openRawResource function.
I am trying to parse an xml file withSaxParser on Android.
This is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
<car model="CitroenC3">
<maintenances>
<xm:maintenance xmlns:xm="it.a.b.android.c.car.m" distance="" price="">
<xm:type></xm:type>
</xm:maintenance>
</maintenances>
<chargings>
<xc:charging xmlns:xc="it.a.b.c.fuelconsumption.car.m" quantity="18" price="20" distance="400" consumption="14"/>
</chargings>
</car>
</cars>
And this is the code:
// Handling XML
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XmlResourceParser parser = getResources().getXml(R.xml.data);
// Create handler to handle XML Tags ( extends DefaultHandler )
DataSaxHandler myXMLHandler = new DataSaxHandler();
xr.setContentHandler(myXMLHandler);
InputStream is= getResources().openRawResource(R.xml.data);
xr.parse(new InputSource(is));
After xr.parse I have the Exception:
03-22 15:24:04.248: INFO/System.out(415): XML Pasing Excpetion =
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: not well-formed (invalid token)
What may be wrong?
Thanks a lot.
AFAIR, any xml file under res/ folder is compiled before it's placed in .apk.
Try to move your XML-file to assets/ folder and load it from there:
xr.parse(new InputSource(getAssets().open("data.xml")));