gpt4 book ai didi

php - 用PHP获取嵌套的XML元素

转载 作者:行者123 更新时间:2023-12-03 16:51:45 25 4
gpt4 key购买 nike

我正在尝试几天,但我找不到正确的代码..我非常拼命,希望我能帮助我。

这是我的XML代码:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="repo.name" name="Repo name" version="1.1.0.1" provider-name="provider name">
<extension point="xbmc.addon.repository" name="repo name">
<info compressed="false">http://data.com/addons.xml</info>
<checksum>http://data.com/addons.xml.md5</checksum>
<datadir zip="true">http://data.com/files</datadir>
</extension>
<extension point="xbmc.addon.metadata">
<description lang="en">bla bla</description>
<disclaimer lang="en">bla 2 bla 2</disclaimer>
<language></language>
<platform>all</platform>
<license>GNU GENERAL PUBLIC LICENSE. Version 2, June 1991</license>
</extension>
</addon>


我如何在PHP中获得以下值:

<extension point="xbmc.addon.metadata">
<description lang="en">bla bla</description> <---- This one


我尝试了诸如foreach循环和xpath之类的东西,但没有任何东西让我使用:bla bla bla

 foreach ($success->summary as $success2) {

echo $success2['lang'];

foreach ($success2->attributes()['lang'] as $success3) {


print_r($success3->summary);


foreach($xml->xpath('extension/@point') as $link) { echo $link['platform']; echo "\n"; }


我希望有人能帮助我,这样我才能完成我的小项目

编辑:

感谢@ Mega6382的帮助。

但是,当我尝试从描述中获取多种语言时,它将无法正常工作:

XML就像:

   <extension point="xbmc.addon.metadata">
<description lang="en">en bla bla</description>

<description lang="nl">Dutch desc</description>
<description lang="be">English desc</description>
<description>Standard desc</description>

</extension>


但是当我尝试使用荷兰语和英语lang时,我只会得到英语描述(当我在en上方移动nl时,我只会得到nl描述。)

邮递区号:

<?php   

$xml = simplexml_load_string ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="repo.name" name="Repo name" version="1.1.0.1" provider-name="provider name">
<extension point="xbmc.addon.repository" name="repo name">
<info compressed="false">http://data.com/addons.xml</info>
<checksum>http://data.com/addons.xml.md5</checksum>
<datadir zip="true">http://data.com/files</datadir>
</extension>
<extension point="xbmc.addon.metadata">
<description lang="en">en bla bla</description>

<description lang="nl">Dutch desc</description>
<description lang="de">German desc</description>
<description>Standard desc</description>

</extension>
</addon>');

foreach($xml->extension as $extension)
{
if(isset($extension->description))
{
if($extension->description['lang'] == 'en')
{
echo $extension->description;
}
if($extension->description['lang'] == 'nl')
{
echo $extension->description;
}

}
}




?>

最佳答案

尝试以下方法:

$xml = simplexml_load_string ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="repo.name" name="Repo name" version="1.1.0.1" provider-name="provider name">
<extension point="xbmc.addon.repository" name="repo name">
<info compressed="false">http://data.com/addons.xml</info>
<checksum>http://data.com/addons.xml.md5</checksum>
<datadir zip="true">http://data.com/files</datadir>
</extension>
<extension point="xbmc.addon.metadata">
<description lang="en">bla bla</description>
<disclaimer lang="en">bla 2 bla 2</disclaimer>
<language></language>
<platform>all</platform>
<license>GNU GENERAL PUBLIC LICENSE. Version 2, June 1991</license>
</extension>
</addon>');

$description = [];
$descriptionEng = null;
foreach($xml->extension as $extension)
{
if(isset($extension->description))
{
if($extension->description['lang'] == 'en')
{
$descriptionEng = $extension->description;
continue;
}
$description[] = $extension->description;
}
}

var_dump($description);


这将遍历所有 <extension>标记,并将检查 <description>标记并将其存储在 $description数组中。您可以通过 print_r($description[0])之类的方式访问其中之一

编辑: $descriptionEng将包含带有 <description>lang=en标记,而 $description将包含其余标记。

编辑2:
尝试以下操作以获取同一扩展名的描述。

$descriptionEng = null;
$descriptionDe = null;
$descriptionNl = null;
foreach($xml->extension as $extension)
{
if(isset($extension->description))
{
foreach($extension->description as $description)
{
if($description['lang'] == 'en')
{
$descriptionEng = $description;
}
elseif($description['lang'] == 'de')
{
$descriptionDe = $description;
continue;
}
elseif($description['lang'] == 'nl')
{
$descriptionNl = $description;
}
}
}
}

关于php - 用PHP获取嵌套的XML元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46697361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com