PHP XPath:如何获取元素内容?

时间:2010-12-07 11:19:21

标签: php xml simplexml

我需要一些PHP / Xpath帮助。任何人都可以给我示例代码吗?

以下是Amazon Web Service中与图书类别相关的XML片段(在本例中,该书属于2类)。我试过走这棵树以我想要的格式获取我需要的信息,但没有取得多大成功(使用SimpleXMLElement)。

我需要的结果是一个嵌套的关联数组,如下所示:

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

这是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>

2 个答案:

答案 0 :(得分:0)

你可以用 hard 的方式来做。

您可以看到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);

答案 1 :(得分:0)

想出来:

$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;
}