I have been trying to convert an xml string into a document so I can later add a node, I have found the same solution in 5 different places however my IDE is giving me an error. The error reads as "the method parse(inputstream)in type DocumentBuilder is not applicable for the arguments(inputSource)."
I have tried the exact code multiple times including choosing the right method output, but I keep getting the same error.
private static Document converStringToDocument(String xml) {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new InputSource(new StringReader(xml)));
return document;
}
I want this to return a document so I can add a node.
I have this XML code:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="https://www.cvlkra.com/">tTKyEndh0iBqnZdjpUntEQ%3d%3d</string>
I want to get this: tTKyEndh0iBqnZdjpUntEQ%3d%3d for which I have tried the below code:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder1 = factory.newDocumentBuilder();
Document document = builder1.parse(new InputSource(new StringReader(string)));
Element rootElement = document.getDocumentElement();
String nodeName = rootElement.getNodeName();
But i am not getting it. I am getting null value instead of tTKyEndh0iBqnZdjpUntEQ%3d%3d even when I have tried some other code also.
Try using getTextContent() instead getNodeValue() returns null because it has no values.
You should not use getNodeName() instead use rootElement.getNodeValue(). May be this helps.
Here's the XML I'm trying to parse: http://realtime.catabus.com/InfoPoint/rest/routes/get/51
#Override
protected Void doInBackground(String... Url) {
try {
URL url = new URL(Url[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Download the XML file
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
// Locate the Tag Name
nodelist = doc.getElementsByTagName("VehicleLocation");
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
During runtime, when it reaches this line: DocumentBuilder db = dbf.newDocumentBuilder(); I get the following error:
Unexpected token (position:TEXT {"RouteId":51,"R...#1:1298 in java.io.InputStreamReader#850b9be)
It seems to have something to do with the encoding. My guess is that it's because the XML doesn't sepcify the encoding, but maybe not.
Is there a way to specify the encoding in the code (I can't change the XML itself)?
Thanks!
EDIT: This seems to only happen when parsing the XML from the url. Storing the file locally seems to work fine.
Is there a way to specify the encoding in the code (I can't change the
XML itself)?
You can call InputSource.setEncoding() to set the encoding.
I would suggest to take a look at XmlPullParser instead for parsing XML in Android.
In my software I'm trying to create a class that returns any bit of an xml file, take a look at the code and the error its giving me, i cant figure out how to correct it :(
Xml :
<everConfigured>
<value>false</value>
</everConfigured>
<ServerPort>
<value>9000</value>
</ServerPort>
<ClientPort>
<value>8000</value>
</ClientPort>
XML Reader Class :
public static String getValue(String Path,String Tag,String Atribute) throws IOException, SAXException, ParserConfigurationException
{
File fXmlFile = new File(Path);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName(Tag);
Node nNode = nList.item(0);
Element eElement = (Element) nNode;
return eElement.getAttribute(Atribute);
}
And here is how I called it :
public static void main(String[] args) throws SocketException, IOException, SAXException, ParserConfigurationException {
System.out.println(
XMLRead.getValue("/home/ghhwer/Desktop/settings.xml", "everConfigured","value"));
}
but returns this error :
[Fatal Error] settings.xml:5:2: The markup in the document following the root element must be well-formed.
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/home/ghhwer/Desktop/settings.xml; lineNumber: 5; columnNumber: 2; The markup in the document following the root element must be well-formed.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
at program.XMLRead.getValue(XMLRead.java:20)
at program.Start.main(Start.java:12)
The markup in the document following the root element must be well-formed.
There are several rules for XML to be well-formed:
All XML Elements Must Have a Closing Tag;
Tags should be in the same case;
XML Elements Must be Properly Nested;
XML Documents Must Have a Root Element;
XML Attribute Values Must be Quoted;
Some symbols have special meaning and have to be escaped (>, <, &, ', ").
In provided XML snippet root element is missing, that is why parser complains.
So, well-formed XML will be:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<everConfigured>
<value>false</value>
</everConfigured>
<ServerPort>
<value>9000</value>
</ServerPort>
<ClientPort>
<value>8000</value>
</ClientPort>
</config>
See http://www.w3schools.com/xml/xml_syntax.asp as XML syntax reference.
I think the problem is that in XML there can be only one root element, but you have three.
You have to restructure you file to something like:
<?xml version="1.0" encoding="UTF-8"?>
<OneRootElement>
<everConfigured>
<value>false</value>
</everConfigured>
<ServerPort>
<value>9000</value>
</ServerPort>
<ClientPort>
<value>8000</value>
</ClientPort>
</oneRootElement>
The error about well-formed XML on the root level should disappear
The problem is an XML validation error, to fix it run the file through an online validation tool like http://www.xmlvalidation.com/ or validate it using the IDE.
The tool will pinpoint the cause better, in this case it seems to be a problem of not well formed XML.
I got a situation when i need to convert my string into xml which contains
"CGNAME="<Input>GROUP1</Input>"
"<SOLRQUERY><I M="1" V="(1:2)OR(62:12)" CGNAME="<Input>GROUP1</Input>" BSLFLG="0" /></SOLRQUERY>"
in parameter I am passing above string
String escaped = StringEscapeUtils.escapeJava(value);
Document CGXml = XPathReader.StringToXMLDocument(escaped);
And i am getting following error:
org.xml.sax.SAXParseException: Open quote is expected for attribute
XPathReader is my inBuilt class and StringToXMLDocument function code i am putting.
public static Document StringToXMLDocument(String criteriaXML) throws ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource isCriteriaXML = new InputSource();
isCriteriaXML.setCharacterStream(new StringReader(criteriaXML));
Document doc = db.parse(isCriteriaXML);
doc.getDocumentElement().normalize();
return doc;
}
You can escape your characters. This means add \ before your "
For example:
CGNAME\"=\"GROUP1\"
So your String query would be:
"<SOLRQUERY>
<I M=\"1\" V=\"(1:2)OR(62:12)\" CGNAME=\"<Input>GROUP1</Input>\" BSLFLG=\"0\" />
</SOLRQUERY>"