嘿,基本上得到了一个庞大的 XML 文档,其中有很多节点、属性值和所有这些爵士乐。
我想变得非常懒,因为我对 XML/PHP 很糟糕,而谷歌这次似乎让我失望了。
是否可以获取 XML 文档中的每个元素并将其设为 PHP 变量?
因此,如果我的 XML 文档就像也许...
<wrap>
<sub></sub>
<subsub></subsub>
<sub></sub>
<subsub></subsub>
<sub2></sub2>
<sub3 value=''></sub3>
<sub4 value=''></sub4>
</wrap>
那么它会递归地检查文档,将其全部转换为 php 变量,无论标签/节点的名称是什么。例如,他们最终看起来可能会像这样。
$换行;
$sub1;
$子[2];
$sub2;
$sub3;
$sub4;
$sub3['值'];
$sub4['值'];
所以我必须聪明地深入研究可能有 8 个孩子吗?
这是可能的还是我必须输入类似
$sub1 = $xml->wrap->sub1;因为这就像...超过 1k 行代码,我不想存在哈哈。
以防万一 xml feed 位于此处
感谢您的宝贵时间:)
Hey, Basically got a massive XML document, with lots of nodes n attributes values and all that jazz.
I'm wanting to be VERY lazy as I'm awful at XML/PHP and google seems to have failed me on this occasion.
if it possible to grab EVERY element in the XML document and make it a PHP variable?
So if my XML Doc was like maybe...
<wrap>
<sub></sub>
<subsub></subsub>
<sub></sub>
<subsub></subsub>
<sub2></sub2>
<sub3 value=''></sub3>
<sub4 value=''></sub4>
</wrap>
so it would recursively check the document converting it all into php variables no matter what the name of the tags/nodes. so for example they would end up looking like maybe.
$wrap;
$sub1;
$sub[2];
$sub2;
$sub3;
$sub4;
$sub3['value'];
$sub4['value'];
so it would have to me smart to delve deep into possibly like 8 children down?
is this possible or will i have to type out like
$sub1 = $xml->wrap->sub1; because that's like...over 1k lines of code that i don't want to exist lol.
just incase the xml feed is HERE
thanks for your time :)
发布评论
评论(3)
然后您可以像这样引用 $xml 对象中的元素
you can then refer to elements within the $xml object like this
您建议的方法确实没有意义,因为您最终会得到与原始 XML 一样复杂的内容,并且需要花费同样多的精力来解析。因此,使用 DOM 解析器 或 SimpleXML 可能是一种明智的继续方式。
也就是说,我怀疑像 xpath (DOM 或 SimpleXML)也可能会有所帮助。不必太关心大量的 XML。
Your suggested approach really doesn't make sense, as you'll just end up with something that's as complex as the originating XML is and requires just as much effort to parse. As such, using the DOM parser or SimpleXML is probably a sane way to proceed.
That said, I suspect something like xpath (DOM or SimpleXML) might also help, if you're only attempting to extract certain information and aren't too concerned about the bulk of the XML.
一旦将 xml 数据加载到 simpleXML 元素中,您就可以非常轻松地访问它。例如:
尽管要小心 sub,因为它是一个保留字。您可能必须将其括在括号中 $xml->{sub}
读取 http://www.php.net/manual/en/simplexml.examples-basic.php
您将必须使用 DOM 对象来执行更复杂的操作,例如删除子项。
once you load the xml data into a simpleXML element, you can access it super easily. for instance:
although be careful with sub as it is a reserved word. you may have to encase it in brackets $xml->{sub}
read http://www.php.net/manual/en/simplexml.examples-basic.php
you will have to use a DOM object to do more complicated stuff like removing children.