2016-03-07 135 views
1

我试图做一些按参数排序。我有这样的XML文件:PHP:按属性过滤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。

回答

6

的问题的答案/标题(过滤器通过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 = 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>'; 
+0

谢谢你FirstOne!我还通过循环中的IF语句找到了解决方案,但您的服务器资源更简单高效。 –

+0

@AndrewBro,是的,我虽然循环它,但'$ result [0]'感觉有点奇怪xD。无论如何,我很乐意帮助.. – FirstOne

+0

是的,使用$ result [0]不是最好的方法)) –