-
Problem with XML input stream
Hi All,
I am trying to connect to an XML connector via http to send text messages... i can connect and send messages fine, but when i take the response from the server i am connecting to (which is XML) and try to parse it i keep getting errors...
I am trying to use JDOM to read the servers response and make sense of it, i am using the following code :
InputStream in = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
outStream.close();
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = builder.build(reader);
}
catch(Exception ex) {
return "Error on making xml " + ex.getMessage();
}
When it executes i get the following error:
Error on making xml Error on line 5: The processing instruction target matching "[xX][mM][lL]" is not allowed
Now i believe that the inputstream is coming with a few trailing blanks, which is maybe what causes the error above?
How would i go about solving this problem?
cheers
Edd
-
reprogram the server so it stops sending you duff information
-
unfortunately i can't reprogramme the server as its a third party supplier and so not under my control!
-
looks like you might have to filter the rubbish that it is sending you then, but firstly i would just examine what it is actually sending:
Code:
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
outStream.close();
//dump all the response to the console
for(String line = reader.readLine(); line!=null; line = reader.readLine()){
System.out.println("]"+line+"[");
}
//SAXBuilder builder = new SAXBuilder();
//Document doc = null;
//try {
//doc = builder.build(reader);
//}
//catch(Exception ex) {
//return "Error on making xml " + ex.getMessage();
//}
the response appears between ] and [ so if you are seeing:
then you know that it is malformed
-
additional:
Code:
int i=0;
for(String line = reader.readLine(); line!=null; line = reader.readLine()){
System.out.println(++i+":]"+line+"[");
}
proiduces line numbers too.. remember line 5 is the problem..
-
the problem is that its sending blanks before the <xml declaration...
I captured the output to a file and then removed the leading spaces and then parsed the file and it worked fine..
i just dont know how to trim the leading spaces from the inputstream before i parse it
-
actually its carriage returns AND spaces that appear before the <xml declaration
this is soooo frustrating
-
you need to implement your own Reader, make it an extension of BufferedReader.
Your new reader should be called TrimmingBufferedReader, and the readLine() method should look like this:
Code:
public String readLine(){
return super.readLine().trim();
}
then replace the reader you have already, with your new one..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks