PHP:按属性过滤XML?

时间:2016-03-07 14:30:03

标签: php xml

我试图通过参数进行某种排序。我有这样的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和所需的attr。

1 个答案:

答案 0 :(得分:7)

问题/标题的答案(按属性过滤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结尾的网址(包含lvl = 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>';