XML String parsing in Android? - android

I use the following code to parse the XML file.
DocumentBuilderFactory factory;
DocumentBuilder builder;
InputStream is;
Document dom;
try {
factory = DocumentBuilderFactory.newInstance();
is = new FileInputStream(strFileName);
builder = factory.newDocumentBuilder();
dom = builder.parse(is);
}
catch(Exception e){}
Instead of XML file is there any way to parse the String.
String xml="<?xml version="1.0"?> <name> Application</name> <demo> Demo </demo> </xml>";

You can convert your string to an InputStream using ByteArrayInputStream:
String xml ="valid xml here";
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
dom = builder.parse(is);

You can use StringReader :
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
Document d = builder.parse(is);

Related

Android dom parser - illegal characters exception

I need to parse a xml document in my Android application and I'm using Dom parser. Encoding in my xml file is set to UTF-8. The code I'm using for parsing is as follows:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream inStream = getAssets().open("words.xml");
InputSource inSource = new InputSource(inStream);
inSource.setEncoding("UTF-8");
Document doc = db.parse(inSource);
But the problem is that I get an illegal character exception. The node which is problematic has the following structure:
<obriši>
<item>obriši</item>
<item>ukloni</item>
</obriši>
What could be the problem?
Try with
inSource.setEncoding("windows-1251");

Parsing XML file stored in internal storage using DOM parser in android.

I have created an xml file in the device's internal storage as described on the android developers website. I now want to parse the file using DOM parser. What do i need to do to make the DOM parser read my XML file??
Here's a snippet:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(new InputSource(new StringReader(data)));
dom.getDocumentElement().normalize();
What do i need to put in the place of "data" in:
Document dom = db.parse(new InputSource(new StringReader(data)));
I know it's silly but any help would be appreciated.
You can make a input stream of the xml string like below and then getting nodes you can parse to get values.
InputStream is = new ByteArrayInputStream(theXMLString.getBytes("UTF-8"));
// Build XML document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
Remember you are passing xml file as a string.
You can give FileInputStream in inputsource
Document dom = db.parse(new InputSource(new FileInputStream(data)));
For reading XML file, you should try below
FileInputStream in = new FileInputStream("/sdcard/text.txt");
StringBuffer data = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text);
inLine.append("\n");
}
in.close();
String finalData =data.toString(); // Here is your data.
Hope above may useful to you.
Try this code for parsing from Asset folder using DOM Parser :
DocumentBuilderFactory DBF;
DocumentBuilder DB;
Document dom;
Element elt;
DBF = DocumentBuilderFactory.newInstance();
DB = DBF.newDocumentBuilder();
dom = DB.parse(new InputSource(getAssets().open("city.xml")));
elt = dom.getDocumentElement();
NodeList items = elt.getElementsByTagName("item");
where item is Node element, add try ctch block as per the requirements.

class cast exception on using xmlresouceparser with Document

Hi all i have a class cast exception, No idea what is this, Can any one help on this
XmlResourceParser fXmlFile = mn.getResources().getXml(R.layout.savecredentials);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse((InputStream) fXmlFile); //exception is on //this line
You're casting an XmlResourceParser
XmlResourceParser fXmlFile = mn.getResources().getXml(R.layout.savecredentials);
to an InputStream:
docBuilder.parse((InputStream) fXmlFile);

How to get String content of Document object(including markup)?

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
//here append some children....
(this application should run on andoroid API 4)
the problem is : how can I get the text content(including markup) of the "doc" ?
I will appreciated if someone can give me some advice. Thanks~!
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
t.transform(new DOMSource(d), new StreamResult(sw));
System.out.println(sw.toString());

How to parse a xml type HTTPResponse in Android

I have an android application where I use POST method to get a response. here is my code:
..........................................
HttpResponse httpResponse = httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
this works fine and i get a response in xml format but i want to parse that xml file and get the node values. i tried this :
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource());
doc.getDocumentElement().normalize();
but I don't know what should give to new InputSource() because I have to use a XML type HTTPResponse not a url.
Thanks !!!
ok thanks everyone for the replies. i just found out a way to overcome my problem.
http://www.rgagnon.com/javadetails/java-0573.html
try
InputStream is = resEntity .getContent()
Document doc = db.parse(is);
You can use any XML parser like SAX to parse ur XML Data
if your response in xml string format follow
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is=new InputSource();
is.setCharacterStream(new StringReader(xmlString(ur response)))
Document doc = db.parse(is);
getsyncFlag(String feedData) {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this);
InputSource is=new InputSource();
is.setCharacterStream(new StringReader((feedData)));
xr.parse(is);
}

Categories

Resources