Android XML Parsing error: Only one root element allowed - java

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>";

Related

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();
}

Read xml file which has 2 root node?

input file
xml
<?xml version="1.0" encoding="UTF-8"?>
<response>
<message></message>
<messagecode></messagecode>
<messagedescription></messagedescription>
</response>
<response>
<message></message>
<messagecode></messagecode>
<messagedescription></messagedescription>
</response>
two response --
response id root node..
Java code
public void readXML(String output) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(output);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("response");
for (int i = 0; i < nodes.getLength(); i++) {
Node nNode = nodes.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) nNode;
NodeList msg = element.getElementsByTagName("message");
Element line = (Element) msg.item(i);
System.out.println("Message: " + getCharacterDataFromElement(line));
NodeList msgcode = element.getElementsByTagName("messagecode");
line = (Element) msgcode.item(i);
System.out.println("Message Code: " + getCharacterDataFromElement(line));
NodeList msgdes = element.getElementsByTagName("messagedescription");
line = (Element) msgdes.item(i);
System.out.println("Message Description: " + getCharacterDataFromElement(line));
NodeList medialink = element.getElementsByTagName("medialink");
line = (Element) medialink.item(i);
System.out.println("Media link: " + getCharacterDataFromElement(line));
NodeList mediastatus = element.getElementsByTagName("mediastatus");
line = (Element) mediastatus.item(i);
System.out.println("Media Status: " + getCharacterDataFromElement(line));
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
i try this code but error will display how can i reslove this....
SAX Exception will give error in not well formed xml file.
how can i read two root node values in same java file..
Your XML does not have matching opening/closing tags or a root node.
Something like this would suffice:
<?xml version="1.0" encoding="UTF-8"?>
<responses>
<response>
<message></message>
<messagecode></messagecode>
<messagedescription></messagedescription>
</response>
<response>
<message></message>
<messagecode></messagecode>
<messagedescription></messagedescription>
</response>
</responses>
XML should always be well-formed, meaning that it has only one root node and every opening tag should have a closing tag. Check for correct XML syntax from W3Schools pages: http://www.w3schools.com/xml/xml_syntax.asp There are also lot's of other useful information about constructing XML documents there.
If then for some reason you absolutely need to handle XML with multiple root nodes, then it's probably manual parsing with substrings or regular expressions, which i don't recommend.
Either way, if you can do something about it, try to form the XML so that you have only one root node. Period.

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());

getNodeName() operation on an XML node returns #text

<person>
<firstname>
<lastname>
<salary>
</person>
This is the XML I am parsing. When I try printing the node names of child elements of person,
I get
text
firstname
text
lastname
text
salary
How do I eliminate #text being generated?
Update -
Here is my code
try {
NodeList nl = null;
int l, i = 0;
File fXmlFile = new File("file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
dbFactory.setValidating(false);
dbFactory.setIgnoringElementContentWhitespace(true);
dbFactory.setNamespaceAware(true);
dbFactory.setIgnoringComments(true);
dbFactory.setCoalescing(true);
InputStream in;
in = new FileInputStream(fXmlFile);
Document doc = dBuilder.parse(in);
doc.getDocumentElement().normalize();
Node n = doc.getDocumentElement();
System.out.println(dbFactory.isIgnoringElementContentWhitespace());
System.out.println(n);
if (n != null && n.hasChildNodes()) {
nl = n.getChildNodes();
for (i = 0; i < nl.getLength(); i++) {
System.out.println(nl.item(i).getNodeName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
setIgnoringElementContentWhitespace only works if you use setValidating(true), and then only if the XML file you are parsing references a DTD that the parser can use to work out which whitespace-only text nodes are actually ignorable. If your document doesn't have a DTD it errs on the safe side and assumes that no text nodes can be ignored, so you'll have to write your own code to ignore them as you traverse the child nodes.

Android remote XML parse (document builder)

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,

Categories

Resources