Problem with XML input stream


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Problem with XML input stream

  1. #1
    Join Date
    Jun 2004
    Posts
    4

    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

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    reprogram the server so it stops sending you duff information
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Jun 2004
    Posts
    4
    unfortunately i can't reprogramme the server as its a third party supplier and so not under my control!

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    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:

    Code:
    ]<xml >   [
    then you know that it is malformed
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    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 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Jun 2004
    Posts
    4
    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

  7. #7
    Join Date
    Jun 2004
    Posts
    4
    actually its carriage returns AND spaces that appear before the <xml declaration


    this is soooo frustrating

  8. #8
    Join Date
    Feb 2004
    Posts
    808
    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..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center