PHP Parse XML from a url

0

is the first time that I parse an XML from PHP and I have many doubts.

I have written this code but it gives me some warning that I do not know if they are normal:

$url = "http://...xml_obras.php";
if(!$xml = simplexml_load_string($url))
{
    echo "No se ha podido cargar el archivo";
} 
else 
{
    echo "El archivo se ha cargado correctamente";
}

and in the url if I give to see source code I get this:

<obra>
      <ninventario>197</ninventario>
      <titulo>Blanca</titulo>
      <autor>ALBERS</autor>
      <fecha>1947</fecha>
</obra>
<obra>
      <ninventario>198</ninventario>
      <titulo>Alfa</titulo>
      <autor>Jose</autor>
      <fecha>1954</fecha>
</obra>

and without giving source code:

197 Blanca ALBERS 1947 198 Alfa Jose 1954

These are the warning:

SCREAM: Error suppression ignored for
( ! ) Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '&lt;' not found in
     

D: \ wamp \ www \ uploadXML.php on line 4

SCREAM: Error suppression ignored for
( ! ) Warning: simplexml_load_string(): http://.../xml_obras.php in D:\wamp\www\cargaXML.php on line 4

What am I doing wrong? Can you tell me how I have to do the foreach for the insert? Thank you very much

    
asked by wiki 02.03.2018 в 18:58
source

1 answer

1

Your xml needs a header so that the browser knows that it is xml, and also the first node that groups them, should work as follows

<?xml version="1.0"?>
<obras>
  <obra>
    <ninventario>197</ninventario>
    <titulo>Blanca</titulo>
    <autor>ALBERS</autor>
    <fecha>1947</fecha>
  </obra>
  <obra>
    <ninventario>198</ninventario>
    <titulo>Alfa</titulo>
    <autor>Jose</autor>
    <fecha>1954</fecha>
  </obra>
</obras>
    
answered by 02.03.2018 / 19:02
source