开发者

- vs _ with simplexml_load_file()

开发者 https://www.devze.com 2023-01-29 00:01 出处:网络
I am trying to parse an xml file, the parent and children use - instead of _ which I\'m used to. <the-parent>

I am trying to parse an xml file, the parent and children use - instead of _ which I'm used to.

<the-parent>
   <the-children>Value</the-children>
</the-parent>

I have been using simplexml_load_file() to parse past xml files and putting it through a foreach() loop to echo everything back out.

$xml = simplexml_load_file($url);

foreach($xml->the-parent as $parent) {
   echo $parent->the-children;
}

I keep getting this error Warning: Invalid argument supplied for foreach()

I can't change the format of the xml file because I am getting it from a 3rd-party. What are my optio开发者_运维百科ns for parsing this correctly?


You can do something like $xml->{'the-parent'}.

See example #3: http://www.php.net/manual/en/simplexml.examples-basic.php


You can use $x->{'the-parent'} and $parent->{'the-children'} to access those values.


I would suggest reading the file into a string variable, replace the "-" chars and after that load the string with simplexml_load_string()

otherwise something like that should work:

$children = $xml->children();
foreach ($children as $element => $value) {
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消