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

发布于 2024-10-06 13:36:14 字数 2642 浏览 4 评论 0原文

我需要一些 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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇女薄笑 2024-10-13 13:36:14

您可以用困难的方式做到这一点。

您可以在此处查看结果

<?
$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);

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);
夏花。依旧 2024-10-13 13:36:14

想通了:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文