简体   繁体   中英

How to use xml_parse_into_struct()

I have a am trying to use xml_parse_into_struct() , but it is returning false

My code looks like this

$p = xml_parser_create();
if (xml_parse_into_struct($p,$contenido,$vals, $index))
{
    print_r($vals);
}

the content of variable

$contenido looks like this

<saltest31>
    <anuncio1>
        <id>12346</id>
        <titulo> BD authority</titulo>
        <descripcion>Preciosa casa en jardines de las animas, en xalapa veracruz. 550 m2 de terreno la mayoria jardin con arboles frutales, amplias recamaras, cocina, salon de juegos, terraza y palapa CVICA123</descripcion>
        <fecha>11/12/2014 10:27:15</fecha>
        <municipio>Chalchuapa</municipio>
        <moneda>Peso</moneda>
        <construccion>500</construccion>
        <terreno>550</terreno>
        <habitaciones>5</habitaciones>
        <banos>4</banos>
        <imagenes>
                <TOTAL>10</TOTAL>
                <IDIMAGE>5567</imagen_url>
                <IDIMAGE>5568</imagen_url>
                <IDIMAGE>5569</imagen_url>
                <IDIMAGE>5570</imagen_url>
                <IDIMAGE>5571</imagen_url>
        </imagenes>
    </anuncio>
</saltest3>

My problem is that the if statement is not returning true , Any idea where I am going wrong

Thanks in advance

It is because you have too many mis-matched tags in your XML starting right at the top with <saltest31>...</saltest3> . This will cause a 0 to be returned.

In addition xml_parse_into_struct() returns a 1 or a 0 which really shouldn't be mistaken for true and false. From the docs ,

xml_parse_into_struct() returns 0 for failure and 1 for success. This is not the same as FALSE and TRUE , be careful with operators such as ===.

Given that, you should perform the test explicitly:

$p = xml_parser_create();
if (xml_parse_into_struct($p,$contenido,$vals, $index) == 1)
{
    print_r($vals);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM