I am getting the following exception while parsing my xml using dom parser. The url "http://www.xyz.com/ABC.aspx?accessCode=......&vin=GJHHFJHFJHFGF6788&reportType=3" returns a xml for every vin parameter.
Here is the xml returned by above url
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<VINdecode Version="1.0.0" Report_Type="LITE" Date="11/1/2012">
<VIN Number="GJHHFJHFJHFGF6788" Status="SUCCESS">
<Vehicle VINdecode_Vehicle_ID="26870" Model_Year="2004" Make="Volkswagen" Model="Touareg" Trim_Level="V6">
<Item Key="Model Year" Value="2004" Unit="" />
<Item Key="Make" Value="Volkswagen" Unit="" />
<Item Key="Model" Value="Touareg" Unit="" />
<Item Key="Trim Level" Value="V6" Unit="" />
<Item Key="Manufactured in" Value="GERMANY" Unit="" />
<Item Key="Body Style" Value="SPORT UTILITY 2-DR" Unit="" />
<Item Key="Engine Type" Value="3.2L V6 DOHC 24V" Unit="" />
</Vehicle>
</VIN>
</VINdecode>
Here is the code which i use to parse xml returned from the url with vin.
public VIN getVINExpansion(String vin)
{
if(vin.length() != 17)
return null;
VIN vehicle = null;
try
{
String url="http://www.xyz.com/ABC.aspx?accessCode=........&vin="
+ vin + "&reportType=3";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(url); **// I get Exception in this line**
NodeList vinlist = doc.getElementsByTagName("VIN");
// rest goes here
}
catch(Exception e)
{
e.printStackTrace();
}
return vin;
}
When I pass a "vin" parameter to above function from my client side through rpc call, I get correct response. But after few hours (say 4-5) hours when I pass the same vin parameter,I get exception. After that I keep on getting this exception till i restart my tomcat server. After restarting tomcat server, again i get correct response for 4-5 hours till it starts failing.
Exception I get :
[Fatal Error] xml_ABC.aspx?accessCode=.......&vin=GJHHFJHFJHFGF6788&reportType=3:4:6: The processing instruction target matching "[xX][mM][lL]" is not allowed.
org.xml.sax.SAXParseException;
systemId: http://www.xyz.com/ABC.aspx?accessCode=......&vin=GJHHFJHFJHFGF6788&reportType=3; lineNumber: 4; columnNumber: 6; The processing instruction target matching "[xX][mM][lL]" is not allowed.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
I don't know the exact reason why it is failing but the same problem was with me: When I was trying to parse url response xml using DocumentBuilder.parse(url), parsing fails after few trials.
When I fetched response xml using the following function:
public String getHttpGetResponseString(String url) throws Exception
{
HttpClient httpclient = new DefaultHttpClient();
String responseBody ="";
try {
HttpGet httpget = new HttpGet(url);
System.out.println("executing request " + httpget.getURI());
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseBody = httpclient.execute(httpget, responseHandler);
}
finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
return responseBody;
}
And then loaded the xml into dom, i got rid of the exception. Hope this may solve your problem.