繁体   English   中英

PHP:按属性过滤XML?

[英]PHP: filter XML by attribute?

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

如何按属性搜索? 我们的想法是用所需的attr输出所有Item

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

您可以使用SimpleXMLElement类

// 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>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM