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".
Related
I have an XML that actually has url-encoded quote marks and other symbols (" appears as %22, ' as %27 and so on).
I'm trying to parse this page without these symbols, but they still appear as %22 and %27.
This is my code:
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
url = new URL(feed);
xr.setContentHandler(rh);
InputSource is = new InputSource(url.openStream());
is.setEncoding("UTF-8");
xr.parse(is);
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")));
I want to parse the result of a specific URL using Simple Ajax for XML. This is basically my Code:
URL link = new URL(url); // url is just a string representing the url
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(myHandler); // the class of myHandler extends from DefaultHandler
InputStream stream = link.openStream();
InputSource inputSource = new InputSource(stream);
inputSource.setEncoding("ISO-8859-1");
xr.parse(inputSource);
But how can I add a cookie? I know I can add Cookies to HttpClients like so:
BasicClientCookie cookie = new BasicClientCookie("access_token", accessToken);
mHttpClient.getCookieStore().addCookie(cookie);
HttpGet request = new HttpGet("www.reeple.net/xml/login/" + uid);
mHttpClient.execute(request);
But how can I add a cookie to a request, that is handlet by the SAX-Api?
Step #1: Use HttpClient to retrieve the XML as a string, using whatever cookies you want
Step #2: Use SAX to parse the string retrieved by HttpClient
Respected All,
I have to read XML file, for that I use SAXParser and DefaultHandler using method characters(char[] ch, int start, int length) but it gives output with some extra characters such as [] in place of '#13'. someone told me that if I read that string in UTF-8 format then it will remove that all the extra characters. Is it true that I have to read it in UTF-8 format if yes then how I can read it.
Thank You
(Vikram Kadam)
I use this to parse with the SAXparser :
URL url = new URL(urlToParse);
SAXParserFactory spf = SAXParserFactory.newInstance();
// here we get our SAX parser
SAXParser sp = spf.newSAXParser();
// we fuse it to a XML reader
XMLReader xr = sp.getXMLReader();
DefaultHandler handlerContact = new DefaultHandler();
// we give it a handler to manage the various events
xr.setContentHandler(handlerContact);
// and finally we open the stream to the url
InputStream oS = url.openStream();
// and parse it
xr.parse(new InputSource(new InputStreamReader(oS, Charset.forName("utf-8"))));
// to retrieve the list of contacts created by the handler
result = handlerContact.getEntries();
// don't forget to close the resource
oS.close();
I never had any trouble as long as the initial file you are parsing is properly encoded in UTF-8. Check if it is, because sometimes, when you use default configuration of your computer, default is not UTF-8 but ANSI or ISO-8859-1