Android remote XML parse (document builder) - java

i have been traipsing the internet for days and i really need some help, i am trying to parse in an XML document from a web server into a ListView in android, i have worked out how to do it with a local file and that is fine, but no matter what i find whether on stack or other sites it just doesnt seem to work, can anyone help me with this?? i know the page exists and works...ive pulled all my hair out now so pleas help :)
below is my code for the method, this is called after the on create and an onclicklistener for items,also i am using the document builder factory method.
private void xmlparse()
{
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(new URL("URL in here").openConnection().getInputStream());
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("item");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
titles.add(getTagValue(TITLE_1, eElement)); //TITLE_1 is the xml tag title
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
Am i missing something daft or have i missed the ball completely? can anyone point me to infomation which would help or just let me know :)
Cheers!!

I've made which do that task, you should do this following :
//Formattage des strings pour le passage en URL
from = URLEncoder.encode(from, "UTF-8");
to = URLEncoder.encode(to, "UTF-8");
String addressToConnect = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+from+"&destinations="+to+"&mode=driving&units="+unit+"&sensor=false";
//Partie connexion
URL url = new URL(addressToConnect);
HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
connexion.connect();
InputSource geocoderResultInputSource;
geocoderResultInputSource = new InputSource(connexion.getInputStream());
//Partie XML/XPATH
Document geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
XPath xpath = XPathFactory.newInstance().newXPath();
//On s'assure que la requete envoy�e � l'API � bien renvoy�e un STATUS = OK cf. Google API
NodeList nodeListCodeResult = (NodeList) xpath.evaluate("//status", geocoderResultDocument, XPathConstants.NODESET);
etc...
Hope it helps,

Related

Android XML Parsing error: Only one root element allowed

I want to parse an XML. I am posting my XML response below. In the
pre tag I am getting a JSON which i want to print but I am not able to parse with my code. I am posting my code to parse this XML.
private void xmlParsing(String qrCode) {
try {
qrCode = qrCode.replaceAll("[^\\x20-\\x7e]", "");
//loge("qrCode : " + qrCode);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new ByteArrayInputStream(qrCode.getBytes("utf-8")));
Element element = doc.getDocumentElement();
element.normalize();
NodeList nList = doc.getElementsByTagName("head");
loge("--df--nList.getLength()---"+nList.getLength());
for (int i=0; i<nList.getLength(); i++) {
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
<head></head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">{"status":true,"message":"Login Successfull","data":{"user":{"id":2,"name":"Rommy Garg","email":"rommy#signitysolutions.com","user_group_id":"2","company_id":2,"last_login":"2019-05-29 05:48:27","last_logout":"2019-05-28 10:33:39","profile_pic":null,"created_at":"2018-12-20 10:12:23","updated_at":"2019-05-29 05:48:27","sf_reference_id":"0056F00000BqMZSQA3","sf_setup":1},"company_logo":"http:\/\/staging.sales-chap.com\/dist\/uploads\/company\/1545300743.jpg","client_id":1,"client_secret":"IQ09J2BdDuc3lSKUJlQAp8uhCXRq+s2EucsBOb9rfjo="}}</pre>
</body>
but I am getting below error:
org.w3c.dom.DOMException: Only one root element allowed
Well, as the error said, XML only allows a single root element. You could create a fake one around the string you recieve:
qrCode = "<html>" + qrCode + "</html>";

Xml parser with child elements

I have the following xml format and wanted to read the elements in java . 'm very new to xml parsing.
<string xmlns="http://tempuri.org/">
<IDV><exshowroomPrice>48800</exshowroomPrice><idv_amount>46360</idv_amount><idv_amount_min>39406</idv_amount_min><idv_amount_max>53314</idv_amount_max><exshowroomPrice_min>41480</exshowroomPrice_min><exshowroomPrice_max>56120</exshowroomPrice_max><outputmessage></outputmessage></IDV>
<string>
I have added this and after that unable to extract the elements.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(responsebuffer.toString()));
Document doc = db.parse(is);
doc.getDocumentElement().normalize();
System.out.println(doc.getDocumentElement().getTextContent());
NodeList nodes = doc.getChildNodes();
Node no1 = (Node) nodes.item(0);
if (doc.getDocumentElement().getChildNodes().getLength() > 0) {
if (nodes.item(0).getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) nodes.item(0);
NodeList nl =element.getElementsByTagName("exshowroomPrice");
System.out.println(((Node)nl).getNodeValue());
}
}
o/p:<IDV><exshowroomPrice>48800</exshowroomPrice><idv_amount>46360</idv_amount><idv_amount_min>39406</idv_amount_min><idv_amount_max>53314</idv_amount_max><exshowroomPrice_min>41480</exshowroomPrice_min><exshowroomPrice_max>56120</exshowroomPrice_max><outputmessage></outputmessage></IDV>
Kindly help,
Thanks in advance.
I'm not sure I understand your question, but your XML is malformed (it should end with ).
That said, the code to parse your document is correct, now I think the simplest way to extract individual elements would be to use class XPathAPI.
For instance:
Node node = XPathAPI.selectSingleNode(doc, "//string/IDV/exshowroomPrice");
System.out.println(node.getTextContent());
UPDATE:
Actually, XPathAPI is not a standard, but you can use XPath:
XPath xpath = XPathFactory.newInstance().newXPath();
String val = (String) xpath.evaluate("//string/IDV/exshowroomPrice/text()", doc, XPathConstants.STRING);
System.out.println(val);
Finally got the answer.
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(responsebuffer.toString()));
Document document = db.parse(is);
document.getDocumentElement().normalize();
//System.out.println(document.getDocumentElement().getTextContent());
StringBuilder xmlStringBuilder = new StringBuilder();
xmlStringBuilder.append("<?xml version=\"1.0\"?>");
xmlStringBuilder.append(document.getDocumentElement().getTextContent());
ByteArrayInputStream input = new ByteArrayInputStream(xmlStringBuilder.toString().getBytes("UTF-8"));
Document doc = db.parse(input);
NodeList nList = doc.getElementsByTagName("IDV");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println(eElement.getElementsByTagName("exshowroomPrice").item(0).getTextContent());
System.out.println(eElement.getElementsByTagName("idv_amount").item(0).getTextContent());
System.out.println(eElement.getElementsByTagName("idv_amount_min").item(0).getTextContent());
System.out.println(eElement.getElementsByTagName("idv_amount_max").item(0).getTextContent());
System.out.println(eElement.getElementsByTagName("exshowroomPrice_min").item(0).getTextContent());
System.out.println(eElement.getElementsByTagName("exshowroomPrice_max").item(0).getTextContent());
System.out.println(eElement.getElementsByTagName("outputmessage").item(0).getTextContent());
}
}
//getElementsByTagName("exshowroomPrice")
} catch (Exception e) {
e.printStackTrace();
}

Filling Vector from NodeList

I am trying to fill a String Vector by data from a NodeList (which values is String too), but it doesn't work and Vector is still empty.
What am I doing wrong and how to fix it?
Thanks in advance!
Document doc = parseFile(xml);
Vector <String> x = new Vector <>();
NodeList list = doc.getElementsByTagName("Stuff");
for (int i = 0; i < list.getLength(); i++) {
x.addElement(list.item(i).getFirstChild().getNodeValue());
}
public Document parseFile(File file) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = (Document) builder.parse(file);
} catch (Exception e) { e.printStackTrace(); }
return doc;
}
Made mistake in tag name in NodeList list = doc.getElementsByTagName("Stuff");
Looking for wrong "Stuff" :)
Sorry, thank you all for help anyway

Parsing XML into Android Application

I am trying to parse this XML onto my Android Application.
I read quite a few questions on parsing the XML here on stackoverflow, yet I am not sure how to handle a for loop everyone mentioned, in my case.
If someone would be nice enough to give me a working example for this case of XML, I'd be very grateful.
THE EXAMPLE CODE I FOUND ON STACKOVERFLOW
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(*/Insert my URL/*);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName(*/What to write here in my case?/*);
item = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
item[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList itemList = fstElmnt.getElementsByTagName(*/What to write here in my case?/*);
Element itemElement = (Element) itemList.item(0);
itemList = itemElement.getChildNodes();
item[i].setText("item = "
+ ((Node) itemList.item(0)).getNodeValue());
layout.addView(item[i]);
}
}
catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
The above code is NOT an example that I am using. It's just what I found on the internet how to parse XML on Android App. But my XML seems more complex than the ussually posted ones here.
use XmlPullParser for parsing XML in your android application. It is very easy to use and pretty fast.
I use XmlPullParser in all my applications.
Follow this tutorial it has nice example on how to use XmlPullParser.
http://www.vogella.com/tutorials/AndroidXML/article.html

Read inside a Tag using XPath Java

Hye I am new to read XML File using Java my problem is that I have been trying to read an xml and between a specific tag I want to get the required data I am using XPath and my query is:
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[#type='STRING']";
It works fine and my specific Tag to read from is:
<ATTRIBUTE name="Description" type="STRING"> SOME TEXT </ATTRIBUTE>
But I want to read the data inside only these types of Tags so that my output should be:
SOME TEXT
inside the tag!
can somebody help me how can I do this Please I am new to xml reading! Trying my best as:
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[#name='Description' and ./type/text()='STRING']";
But it wont give me any output!
thanks in advance
My Code:
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(
new FileInputStream("c:\\y.xml"));
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[#name='Description'and #type='STRING']";
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
} catch (ParserConfigurationException | SAXException | IOException e) {
System.out.print(e);
}
There is a problem with my code cant figure out what!
This code works fine for me with the changed XPath to:
"/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[#name='Description'][#type='STRING']":
private static final String EXAMPLE_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ADOXML adoversion=\"Version 5.1\" username=\"kvarga\" database=\"adonisdb\" time=\"08:55\" date=\"30.11.2013\" version=\"3.1\">" +
"<MODELS>" +
"<MODEL version=\"\" applib=\"ADONIS BPMS BP Library 5.1\" libtype=\"bp\" modeltype=\"Business process model\" name=\"Product development\" id=\"mod.25602\">" +
"<MODELATTRIBUTES>" +
"<ATTRIBUTE name=\"Version number\" type=\"STRING\"> </ATTRIBUTE>" +
"<ATTRIBUTE name=\"Author\" type=\"STRING\">kvarga</ATTRIBUTE>" +
"<ATTRIBUTE name=\"Description\" type=\"STRING\">I WANT THIS PARA 2</ATTRIBUTE>" +
"</MODELATTRIBUTES>" +
"</MODEL>" +
"</MODELS>" +
"</ADOXML>";
public static void main(String[] args) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(EXAMPLE_XML.getBytes()));
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[#name='Description'][#type='STRING']";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println("###" + nodeList.item(i).getFirstChild().getNodeValue() + "###");
}
} catch (Exception e) {
System.out.print(e);
}
}
OUTPUT:
###I WANT THIS PARA 2###
The mentioned code works fine.
You can try other way also to get the text node -
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE/text()";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
System.out.println(nodeList.item(0).getNodeValue());

Categories

Resources