How can I parse an XML String?

0

I am doing an application on Android, my error is as follows, from the Application, I send a parameter to a query, from a php, within this php, when it makes the query, it generates a response in a way structuring an XML. (I enclose the php so you can see how I have done it.)

    <?php
$link = mysqli_connect("mysql.hostinger.es", "u126083979_admin", "123456", "u126083979_lugar");
     if(isset($_POST['nombre']))
     {
      $nombre = $_POST['nombre'];
      $query1 = "SELECT * FROM Sitio WHERE Nombre = '".$nombre."'";
       if($result = mysqli_query($link, $query1))
       {
             echo "<Sitios>";
             while($Sitio = mysqli_fetch_array($result))
             {
              echo "<Sitio>";
              echo "<Nombre>" . $Sitio['Nombre'] . "</Nombre>";
              echo "<ImagenPrin>" . $Sitio['ImagenPrin'] . "</ImagenPrin>";
              echo "</Sitio>";
             }
             echo "</Sitios>";
             mysqli_free_result($result);

       }
       else
       {
         echo "ERROR: $sql. " . mysqli_error($link);
       }
     }
?>

Starting from my php giving me the answer in the form of xml, in the class I read, this answer in the following way.

int response_code = conexion.getResponseCode();

                if (response_code == HttpURLConnection.HTTP_OK) {
                    // Lee los datos enviados desde el php
                    InputStream input = conexion.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                    StringBuilder result = new StringBuilder();
                    String line;
                    String vble="";
                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                        vble=vble+line;                       
                    }

Now, as I understand it, the answer is stored in the Variable String vble , right now, and what I want right now is to parse the variable. I have it in the following way.

SAXParserFactory factory = SAXParserFactory.newInstance();
                    SAXParser saxParser = factory.newSAXParser();
                    SaxHandlerSitio handler = new SaxHandlerSitio();
                    saxParser.parse(vble, handler);

I do not understand why I am not parsea, finally I add my parse class through SAXHANDLER.

public class SaxHandlerSitio extends DefaultHandler{
    private List<Sitio> sitios;
    private Sitio sitioAc;
    private StringBuilder sbText;
    public Boolean parsingError = false;



    public List<Sitio> getSitios(){
        return sitios;
    }


    @Override
    public void startDocument() throws SAXException {

        super.startDocument();

        sitios = new ArrayList<Sitio>();
        sbText = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {

        super.startElement(uri, localName, name, attributes);

        if (localName.equals("Sitio")) {
            sitioAc = new Sitio(name, 0,0);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        super.characters(ch, start, length);

        if (this.sitioAc != null)
            sbText.append(ch, start, length);
    }


    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {

        super.endElement(uri, localName, name);
        double value;
        int valor = 0;
        if (this.sitioAc != null) {

            if (localName.equals("Nombre")) {
                sitioAc.setNombre(sbText.toString().trim());
            }
            else if (localName.equals("Latitud")) {
                value = Double.parseDouble(sbText.toString().trim());
                sitioAc.setLatitud(value);
            }
            else if (localName.equals("Longitud")) {
                value = Double.parseDouble(sbText.toString().trim());
                sitioAc.setLongitud(value);
            }
            else if (localName.equals("ImagenPrin")) {
                sitioAc.setImagen(sbText.toString().trim());
            }
            else if (localName.equals("Sitio")) {
                sitios.add(sitioAc);
            }
            sbText.setLength(0);
        }
    }

}

These are the mistakes that come to me

04-18 18:04:45.530 12331-20851/com.adilosa94.theturistllion W/System.err: java.io.IOException: Couldn't open <Sitios><Sitio><Nombre>Basílica de San Isidoro</Nombre><ImagenPrin>http://theturistllion.hol.es/upload/Imagenes/Sitios%20Principal/san_isidoro.jpg</ImagenPrin></Sitio></Sitios>
    04-18 18:04:45.533 12331-20851/com.adilosa94.theturistllion W/System.err: Caused by: java.net.MalformedURLException: Protocol not found: <Sitios><Sitio><Nombre>Basílica de San Isidoro</Nombre><ImagenPrin>http://theturistllion.hol.es/upload/Imagenes/Sitios%20Principal/san_isidoro.jpg</ImagenPrin></Sitio></Sitios>
    
asked by Alejandro Díez 19.04.2017 в 19:17
source

1 answer

1

The error tells you that you have tried to open as a URI (or URL) what is actually the XML content.

If we go to the always useful Javadoc, we see that from SAXParser.parse (String, DefaultHandler) says that the first parameter is the URI from which to get the XML to parse.

So, it should be enough to do

 saxParser.parse(laUrlDeMiXml, handler);

and remove the entire method of obtaining the String through the connection.

If for any reason (authentication, control / log, etc.) you want to continue opening the connection / downloading the XML as String, you should use some other of the existing parse methods; for example

saxParser.parse(connection.getInputStream(), handler);

or

saxParser.parse(new InputSource(vble), handler);
    
answered by 19.04.2017 в 20:44