Android Question How to xml saxparse a web page

Discussion in 'Android Questions' started by Alberto Michelis, May 23, 2015.

  1. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    Hi,
    I´ve modifed the SaxXml parser example to read a xml provided by a web page.
    All seams to go well but at the end of the Parsing I got an error like:

    Error occurred on line: 61 (main)
    org.apache.harmony.xml.ExpatParser$ParseException: At line 42, column 0: junk after document element

    Line 61 is the End If of the Parser routine.

    Attached is the XmlSax.zip file with my code

    Thanks
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    Try to open this link in your browser: http://200.68.94.242/xmloc?F=Sxml&S=L&M=PES&Z=1&ES=APBR,ERAR

    You will get the same error message. If you check the page source you will see that the response is not a valid xml string. There is another xml root at the end of the document. You will need to first remove it.
     
  3. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    Thanks I how do I remove this last two lines, I have no control of the html server.
    I am using...

    Sub JobDone(Job As HttpJob)
    Log("***JObDone")
    If Job.Success = True Then
    Log("read OK")
    Log(Job.GetString)
    parser.Parse(Job.GetInputStream, "Parser")
    Else
    Log("error")
    Log(Job.ErrorMessage)
    End If

    End Sub

    Sendig a Job.GetInputStream to the parser
    How can I remove those lines from the GetImputStream?

    Thanks
     
  4. fixit30

    fixit30 Active Member Licensed User

    If you have no control over the Webserver then you will need to strip out the Bad Xml yourself before sending it to XmlSax.

    Something like the below.

    Code:
    Sub JobDone(Job As HttpJob)
        
    Log("***JObDone")
        
    If Job.Success = True Then   
            
    Log("lectura web OK")
             
    Log(Job.GetString)
            parser.Parse(StringToInputStream(Job.GetString), 
    "Parser")
        
    Else   
            
    Log("error de lectura web")
            
    Log(Job.ErrorMessage)
        
    End If

    End Sub

    Sub StringToInputStream (s As StringAs InputStream
       
    Dim In As InputStream
       s = s.Replace(
    "<?xml version=""1.0"" encoding=""iso-8859-1""?>""")
       s = s.Replace(
    "<ajax-response></ajax-response>""")
       
    Dim data() As Byte = s.GetBytes("UTF8")
       
    In.InitializeFromBytesArray(data, 0, data.Length)
       
    Return In
    End Sub
     
  5. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    I'm having problems with this code... please see image, thanks.
     

    Attached Files:

    • b4a.png
      b4a.png
      File size:
      55.9 KB
      Views:
      78
  6. fixit30

    fixit30 Active Member Licensed User

    Are you sure that you have Initialised parser?

    Code:
    parser.Initialize
    The attached modified example that you provided above works fine on my device.
     

    Attached Files:

  7. Alberto Michelis

    Alberto Michelis Active Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice