简体   繁体   English

如何使用xml_parse_into_struct()

[英]How to use xml_parse_into_struct()

I have a am trying to use xml_parse_into_struct() , but it is returning false 我正在尝试使用xml_parse_into_struct() ,但返回的是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 $contenido看起来像这样

<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 我的问题是if语句未返回true,我在哪里出错了

Thanks in advance 提前致谢

It is because you have too many mis-matched tags in your XML starting right at the top with <saltest31>...</saltest3> . 这是因为您的XML中从<saltest31>...</saltest3>开始的顶部有太多不匹配的标签。 This will cause a 0 to be returned. 这将导致返回0。

In addition xml_parse_into_struct() returns a 1 or a 0 which really shouldn't be mistaken for true and false. 另外, xml_parse_into_struct()返回1或0,这不应误认为是非。 From the docs , 文档中

xml_parse_into_struct() returns 0 for failure and 1 for success. xml_parse_into_struct()对于失败返回0,对于成功返回1。 This is not the same as FALSE and TRUE , be careful with operators such as ===. 这与FALSETRUE不同 ,请小心使用===之类的运算符。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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