gpt4 book ai didi

PHP:按属性过滤 XML?

转载 作者:数据小太阳 更新时间:2023-10-29 02:51:05 26 4
gpt4 key购买 nike

我正在尝试按参数进行某种排序。我有这样的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<Root>
<Name>Logos</Name>
<Slides>
<Item file="/upload/portfolio/22.jpg" lvl="1" designer="0001" theme="outline">Destiption one</Item>
<Item file="/upload/portfolio/23.jpg" lvl="1" designer="0002" theme="drawn">Destiption 2</Item>
<Item file="/upload/portfolio/24.jpg" lvl="2" designer="0003" theme="outline">Destiption 3</Item>
<Item file="/upload/portfolio/26.jpg" lvl="2" designer="0004" theme="drawn">Destiption 4</Item>
<Item file="/upload/portfolio/27.jpg" lvl="1" designer="0003" theme="outline">Destiption 5</Item>
<Item file="/upload/portfolio/28.jpg" lvl="3" designer="0003" theme="outline">Destiption 6</Item>
</Slides>
</Root>

PHP:

$result = $xml->Slides->xpath('Item');//get all items
$search1 = $result[0]->xpath('Item[@lvl="1"]');//failed(((

如何按属性搜索?这个想法是输出所有具有所需属性的 Item

最佳答案

问题/标题的答案(按属性过滤 XML):

您可以使用 The SimpleXMLElement class :

// xml example
$foo = '<?xml version="1.0" encoding="utf-8"?>
<Root>
<File type="1">File 1</File>
<File type="2">File 2</File>
<File type="1">File 3</File>
</Root>';

$xml = new SimpleXMLElement($foo);
// get all 'File' elements with attribute 'type' = '1'
$search1 = $xml->xpath('File[@type="1"]');
// ^^^^ ^^
// Element Attribute

然后,$search1 将有文件 File 1File 3



对于 OP 的特定情况: 无需先获取所有项目。您可以像这样直接使用它:

$result = $xml->Slides->xpath('Item[@lvl="1"]'); //get 'Item'-s with 'lvl' '1'

所以,print_r($result); 输出:

Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[file] => /upload/portfolio/22.jpg
[lvl] => 1
[designer] => 0001
[theme] => outline
)

[0] => Destiption one
)

[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[file] => /upload/portfolio/23.jpg
[lvl] => 1
[designer] => 0002
[theme] => drawn
)

[0] => Destiption 2
)

[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[file] => /upload/portfolio/27.jpg
[lvl] => 1
[designer] => 0003
[theme] => outline
)

[0] => Destiption 5
)

)

请注意,它返回以 222327 结尾的 url(以 lvl 结尾的 url) = 1)


如果您查看您的代码,它有 $result[0]->xpath...$result 已经是项目列表,所以你会得到第一个项目 $result[0] (print_r($result[0]) ;):

SimpleXMLElement Object
(
[@attributes] => Array
(
[file] => /upload/portfolio/22.jpg
[lvl] => 1
[designer] => 0001
[theme] => outline
)

[0] => Destiption one
)

完整代码(复制/粘贴运行):

$foo = '<?xml version="1.0" encoding="utf-8"?>
<Root>
<Name>Logos</Name>
<Slides>
<Item file="/upload/portfolio/22.jpg" lvl="1" designer="0001" theme="outline">Destiption one</Item>
<Item file="/upload/portfolio/23.jpg" lvl="1" designer="0002" theme="drawn">Destiption 2</Item>
<Item file="/upload/portfolio/24.jpg" lvl="2" designer="0003" theme="outline">Destiption 3</Item>
<Item file="/upload/portfolio/26.jpg" lvl="2" designer="0004" theme="drawn">Destiption 4</Item>
<Item file="/upload/portfolio/27.jpg" lvl="1" designer="0003" theme="outline">Destiption 5</Item>
<Item file="/upload/portfolio/28.jpg" lvl="3" designer="0003" theme="outline">Destiption 6</Item>
</Slides>
</Root>';

$xml = new SimpleXMLElement($foo);
$search1 = $xml->Slides->xpath('Item[@lvl="1"]');
echo '<pre>';
print_r($search1);
echo '</pre>';

关于PHP:按属性过滤 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846288/

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