自动将整个 XML 文档解析为 PHP 变量

发布于 2024-10-09 22:14:30 字数 1088 浏览 0 评论 0 原文

嘿,基本上得到了一个庞大的 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷清清 2024-10-16 22:14:30
   $xml = simplexml_load_string($response);
// $response is your xml file/output string

然后您可以像这样引用 $xml 对象中的元素

$xml->children('yourschema')->children('yoururn')->Tag1->Tag2
   $xml = simplexml_load_string($response);
// $response is your xml file/output string

you can then refer to elements within the $xml object like this

$xml->children('yourschema')->children('yoururn')->Tag1->Tag2
空袭的梦i 2024-10-16 22:14:30

您建议的方法确实没有意义,因为您最终会得到与原始 XML 一样复杂的内容,并且需要花费同样多的精力来解析。因此,使用 DOM 解析器SimpleXML 可能是一种明智的继续方式。

也就是说,我怀疑像 xpath (DOMSimpleXML)也可能会有所帮助。不必太关心大量的 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.

谁把谁当真 2024-10-16 22:14:30

一旦将 xml 数据加载到 simpleXML 元素中,您就可以非常轻松地访问它。例如:

$xml = new SimpleXMLElement($input_xml_string);
foreach($xml->sub as $value) {
   echo $value->subsub;
}

尽管要小心 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:

$xml = new SimpleXMLElement($input_xml_string);
foreach($xml->sub as $value) {
   echo $value->subsub;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文