开发者

PHP XPath: How to get element content?

开发者 https://www.devze.com 2023-01-29 19:58 出处:网络
I need some PHP/Xpath help. Can anyone give me sample code to follow? The following is a snippet of XML from Amazon Web Service pertaining to the categories of a book (in this case, the book falls un

I need some PHP/Xpath help. Can anyone give me sample code to follow?

The following is a snippet of XML from Amazon Web Service pertaining to the categories of a book (in this case, the book falls under 2 categories). I've tried walking this tree to get the information I need in the format I want, but was not having much success (was using SimpleXMLElement).

The result I need is a nested, associative array that looks like this:

[0]
   - '11079' => 'Politics'
   - '53' => 'Nonfiction'
   - '1000' => 'Subjects'
   - '283155' => 'Books'
[1]
   - '11232' => 'Social Sciences'
   - '53' => 'Nonfiction'
   - '1000' => 'Subjects'
   - '283155' => 'Books'

Here's the XML:

<BrowseNodes>
    <BrowseNode>
        <BrowseNodeId>110开发者_如何学C79</BrowseNodeId>
        <Name>Politics</Name>
        <Ancestors>
            <BrowseNode>
                <BrowseNodeId>53</BrowseNodeId>
                <Name>Nonfiction</Name>
                <Ancestors>
                    <BrowseNode>
                        <BrowseNodeId>1000</BrowseNodeId>
                        <Name>Subjects</Name>
                        <IsCategoryRoot>1</IsCategoryRoot>
                        <Ancestors>
                            <BrowseNode>
                                <BrowseNodeId>283155</BrowseNodeId>
                                <Name>Books</Name>
                            </BrowseNode>
                        </Ancestors>
                    </BrowseNode>
                </Ancestors>
            </BrowseNode>
        </Ancestors>
    </BrowseNode>
    <BrowseNode>
        <BrowseNodeId>11232</BrowseNodeId>
        <Name>Social Sciences</Name>
        <Ancestors>
            <BrowseNode>
                <BrowseNodeId>53</BrowseNodeId>
                <Name>Nonfiction</Name>
                <Ancestors>
                    <BrowseNode>
                        <BrowseNodeId>1000</BrowseNodeId>
                        <Name>Subjects</Name>
                        <IsCategoryRoot>1</IsCategoryRoot>
                        <Ancestors>
                            <BrowseNode>
                                <BrowseNodeId>283155</BrowseNodeId>
                                <Name>Books</Name>
                            </BrowseNode>
                        </Ancestors>
                    </BrowseNode>
                </Ancestors>
            </BrowseNode>
        </Ancestors>
    </BrowseNode>
</BrowseNodes>


You can do it the hard way.

You can see the result here.

<?
$xml = '<BrowseNodes>
    <BrowseNode>
        <BrowseNodeId>11079</BrowseNodeId>
        <Name>Politics</Name>
        <Ancestors>
            <BrowseNode>
                <BrowseNodeId>53</BrowseNodeId>
                <Name>Nonfiction</Name>
                <Ancestors>
                    <BrowseNode>
                        <BrowseNodeId>1000</BrowseNodeId>
                        <Name>Subjects</Name>
                        <IsCategoryRoot>1</IsCategoryRoot>
                        <Ancestors>
                            <BrowseNode>
                                <BrowseNodeId>283155</BrowseNodeId>
                                <Name>Books</Name>
                            </BrowseNode>
                        </Ancestors>
                    </BrowseNode>
                </Ancestors>
            </BrowseNode>
        </Ancestors>
    </BrowseNode>
    <BrowseNode>
        <BrowseNodeId>11232</BrowseNodeId>
        <Name>Social Sciences</Name>
        <Ancestors>
            <BrowseNode>
                <BrowseNodeId>53</BrowseNodeId>
                <Name>Nonfiction</Name>
                <Ancestors>
                    <BrowseNode>
                        <BrowseNodeId>1000</BrowseNodeId>
                        <Name>Subjects</Name>
                        <IsCategoryRoot>1</IsCategoryRoot>
                        <Ancestors>
                            <BrowseNode>
                                <BrowseNodeId>283155</BrowseNodeId>
                                <Name>Books</Name>
                            </BrowseNode>
                        </Ancestors>
                    </BrowseNode>
                </Ancestors>
            </BrowseNode>
        </Ancestors>
    </BrowseNode>
</BrowseNodes>';

$simplexml = simplexml_load_string($xml);
$return = array();

foreach($simplexml->BrowseNode as $node){
 $return[] = array(
    (string)$node->BrowseNodeId => (string)$node->Name,
    (string)$node->Ancestors->BrowseNode->BrowseNodeId => (string)$node->Ancestors->BrowseNode->Name,
    (string)$node->Ancestors->BrowseNode->Ancestors->BrowseNode->BrowseNodeId => (string)$node->Ancestors->BrowseNode->Ancestors->BrowseNode->Name,
    (string)$node->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->BrowseNodeId => (string)$node->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Name,
 );
}

print_r($return);


Figured it out:

$categories = array();
$categories_count = count($simple_xml_emlement->xpath('//BrowseNodes/BrowseNode'));
for($i = 1; $i <= $categories_count; $i++)
{
   $category = array();
   $category_names = $simple_xml_emlement->xpath("//BrowseNodes/BrowseNode[$i]//Name");
   foreach($category_names as $category_name)
   {
      $category_id = $category_name->xpath('preceding-sibling::*');
      $category[(int)$category_id[0]] = (string) $category_name;
   }
   $categories[] = $category;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消