Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: filter XML by attribute?

Tags:

php

xml

Im trying to make some kind of sorting by parameters. I've got XML file like this:

<?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(((

how to search by attribute? The idea is to output all Items with required attr.

like image 891
Andrew Bro Avatar asked Jun 07 '26 01:06

Andrew Bro


1 Answers

The answer to the question / title (filter XML by attribute):

You can use 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

Then, $search1 will have files File 1 and File 3.



For the OP's specific case: There is no need to first get all items. You can use it like this, directly:

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

so, print_r($result); outputs:

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
        )

)

Note that it returns the urls that end with 22, 23 and 27 (the ones with lvl = 1)


If you take a look at your code, it has $result[0]->xpath.... $result is already the list of items, so you'd be getting the first item with $result[0] (print_r($result[0]);):

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

    [0] => Destiption one
)

Complete code (Copy/Paste to run):

$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>';
like image 83
FirstOne Avatar answered Jun 10 '26 12:06

FirstOne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!