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
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 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".
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 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
I just read some tutorials in order to parse a xml feed from the web and turn them into a Listview:
URL file = new URL("http://..../file.xml");
SAXParserFactory fabrique = SAXParserFactory.newInstance();
SAXParser parseur = fabrique.newSAXParser();
XMLReader xr = parseur.getXMLReader();
ReglageParseur gestionnaire = new ReglageParseur();
xr.setContentHandler(gestionnaire);
xr.parse(new InputSource(file.openStream()));
Everything is fine and I am able to parse xml.
My second step is to store the xml file from web into a xml file on the phone and only update it when user ask it. ( In fact, this xml file should not change or maybe once every 6 month, so I don't want to download it each time.)
So, what I did is to store the file on the phone and update it on user demand.
And I can read it by doing:
fIn = openFileInput("fichier.xml");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[255];
isr.read(inputBuffer);
String readString = new String(inputBuffer);
So, for now, everything seem fine and I am nearly happy.
The problem is now when I want to parse the new file on the phone:
xr.parse(InputSource);
I need an InputSource as parameter.
So my question is:
How can I turn my file in the phone into a InputSource?
I succeed to have a InputStreamReader or a String but would like to convert that into InputSource.
Thank a lot for any precious help
Well, I don't know what constructors are available on the Android version, but the J2SE InputSource class has a constructor with a Reader parameter. Have you tried that?
Alternatively, why not just construct an InputSource directly from the InputStream? I assume fIn is a FileInputStream? Why not just call:
InputSource input = new InputSource(fIn);
?
the best suitable line for me to convert string to InputSource is :
String myStringObject = "Hello this is string object to convert in InputSource";
InputSource inSource = new InputSource(new StringReader(myStringObject));