根据子内容解析 xml 属性

发布于 2024-10-09 19:12:30 字数 4055 浏览 0 评论 0原文

我加载一个包含用户元数据的外部 xml 文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<user_information>
<user url="http://usersweb.com">
<name>Arnold</name>
<lastname>
<name>Arnoldson</name>
</lastname>
<age>42</age>
</user>
<user url="http://anotheruserweb.com">
<name>Arnold</name>
<lastname>
<name>Arichson</name>
</lastname>
<age>42</age>
</user>
And so on....
</user_information>

我知道格式化

<lastname> 

确实很愚蠢,但我无法修改源代码。

我想从 加载“url”属性

<user>

,但前提是

<name> 

和 与

<lastname> 

我存储在自己的用户数据库中的名称值匹配。

我知道如何用 PHP 解析其余部分,但仅匹配序列对我来说似乎很困难。任何帮助表示赞赏!


编辑: 由于我无法向您提供源 XML,因此我在 Spotify 元数据 API 中找到了类似的代码结构。我们可以处理从曲目搜索“Domino Dancing”返回的 XML 文件: ws.spotify.com/search/1/track?q=Domino Dancing

它具有完全相同的结构,只是使用了其他单词。返回的文件如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<tracks xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1"> 
<opensearch:Query role="request" startPage="1" searchTerms="Domino Dancing"/> 
<opensearch:totalResults>39</opensearch:totalResults> 
<opensearch:startIndex>0</opensearch:startIndex> 
<opensearch:itemsPerPage>100</opensearch:itemsPerPage> 
<track href="spotify:track:2aV5vCNH5tOOe0GGKqfx5Z"> 
<name>Domino dancing</name> 
<artist href="spotify:artist:2ycnb8Er79LoH2AsR5ldjh"> 
<name>Pet Shop Boys</name> 
</artist> 
<id type="isrc">GBAYE0301704</id> 
<album href="spotify:album:0BAfuhzkGjckMhsL2gVbzR"> 
<name>Ultimate</name> 
<released>2010</released> 
<availability> 
<territories>AT BE CH CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU NL NO PL PT RO SE SI SK</territories> 
</availability> 
</album> 
<track-number>7</track-number> 
<length>258.106000</length> 
<popularity>0.57092</popularity> 
</track> 
<track href="spotify:track:4mVLzFbc3gaCoWCEENLouc"> 
<name>Domino Dancing</name> 
<artist href="spotify:artist:2ycnb8Er79LoH2AsR5ldjh"> 
<name>Pet Shop Boys</name> 
</artist> 
<id type="isrc">GBAYE8800056</id> 
<album href="spotify:album:0Jt2LzWgtGxy3GZH5i2Kcy"> 
<name>Discography - Complete Singles Collection</name> 
<released>1991</released> 
<availability> 
<territories>AT BE CH CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU NL NO PL PT RO SE SI SK</territories> 
</availability> 
</album> 
<track-number>10</track-number> 
<length>258.333000</length> 
<popularity>0.56159</popularity> 
</track> 
<track href="spotify:track:72kgJ1brMxxeDiPNdH6qqW"> 
<name>Viva la vida/Domino dancing</name> 
<artist href="spotify:artist:2ycnb8Er79LoH2AsR5ldjh"> 
<name>Pet Shop Boys</name> 
</artist> 
<id type="isrc">GBCEW0900049</id> 
<album href="spotify:album:4DZfMTDyBwnJQ0k1PaT6U4"> 
<name>Pet Shop Boys Christmas</name> 
<released>2009</released> 
<availability> 
<territories>AT BE CH CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU NL NO PL PT RO SE SI SK</territories> 
</availability> 
</album> 
<track-number>4</track-number> 
<length>333.507000</length> 
<popularity>0.51961</popularity> 
</track>
</tracks>

如果我们假装 用户信息=曲目 用户=跟踪 网址=参考 姓氏=艺术家。

希望这有帮助!

编辑2: 我使用 Xpath Document 和命名空间解决了这个问题。谢谢大家的努力!

I load an external xml file containing user metas looking like this:

<?xml version="1.0" encoding="utf-8"?>
<user_information>
<user url="http://usersweb.com">
<name>Arnold</name>
<lastname>
<name>Arnoldson</name>
</lastname>
<age>42</age>
</user>
<user url="http://anotheruserweb.com">
<name>Arnold</name>
<lastname>
<name>Arichson</name>
</lastname>
<age>42</age>
</user>
And so on....
</user_information>

I know the formatting with

<lastname> 

is really stupid, but i can't modify the source.

I want to load in the "url"-attribute from

<user>

, but only if

<name> 

and

<lastname> 

matches the name values i have stored in my own user database.

I know how to parse the rest of it with PHP, but just the matching sequence seems difficult to me. Any help appreciated!


EDIT:
Since i can not give you the source XML, i found a similar code structure in the Spotify Metadata API. We can laborate with the returning XML-file from the track search "Domino Dancing": ws.spotify.com/search/1/track?q=Domino Dancing

It has the exactly same structur, excepts it use other words. The returning file looks like this:

<?xml version="1.0" encoding="utf-8"?> 
<tracks xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1"> 
<opensearch:Query role="request" startPage="1" searchTerms="Domino Dancing"/> 
<opensearch:totalResults>39</opensearch:totalResults> 
<opensearch:startIndex>0</opensearch:startIndex> 
<opensearch:itemsPerPage>100</opensearch:itemsPerPage> 
<track href="spotify:track:2aV5vCNH5tOOe0GGKqfx5Z"> 
<name>Domino dancing</name> 
<artist href="spotify:artist:2ycnb8Er79LoH2AsR5ldjh"> 
<name>Pet Shop Boys</name> 
</artist> 
<id type="isrc">GBAYE0301704</id> 
<album href="spotify:album:0BAfuhzkGjckMhsL2gVbzR"> 
<name>Ultimate</name> 
<released>2010</released> 
<availability> 
<territories>AT BE CH CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU NL NO PL PT RO SE SI SK</territories> 
</availability> 
</album> 
<track-number>7</track-number> 
<length>258.106000</length> 
<popularity>0.57092</popularity> 
</track> 
<track href="spotify:track:4mVLzFbc3gaCoWCEENLouc"> 
<name>Domino Dancing</name> 
<artist href="spotify:artist:2ycnb8Er79LoH2AsR5ldjh"> 
<name>Pet Shop Boys</name> 
</artist> 
<id type="isrc">GBAYE8800056</id> 
<album href="spotify:album:0Jt2LzWgtGxy3GZH5i2Kcy"> 
<name>Discography - Complete Singles Collection</name> 
<released>1991</released> 
<availability> 
<territories>AT BE CH CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU NL NO PL PT RO SE SI SK</territories> 
</availability> 
</album> 
<track-number>10</track-number> 
<length>258.333000</length> 
<popularity>0.56159</popularity> 
</track> 
<track href="spotify:track:72kgJ1brMxxeDiPNdH6qqW"> 
<name>Viva la vida/Domino dancing</name> 
<artist href="spotify:artist:2ycnb8Er79LoH2AsR5ldjh"> 
<name>Pet Shop Boys</name> 
</artist> 
<id type="isrc">GBCEW0900049</id> 
<album href="spotify:album:4DZfMTDyBwnJQ0k1PaT6U4"> 
<name>Pet Shop Boys Christmas</name> 
<released>2009</released> 
<availability> 
<territories>AT BE CH CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU NL NO PL PT RO SE SI SK</territories> 
</availability> 
</album> 
<track-number>4</track-number> 
<length>333.507000</length> 
<popularity>0.51961</popularity> 
</track>
</tracks>

If we pretend that
user_information=tracks
user=track
url=href
lastname=artist.

Hope this helps!

EDIT2:
I solved it using Xpath Document and namespaces. Thank you all for trying!

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

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

发布评论

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

评论(2

凝望流年 2024-10-16 19:12:30

此 XPATH 选择 Arnold Archison 的 @url

/user_information/user[name='Arnold' and lastname/name='Arichson']/@url

编辑

关于您的解决方案的两个问题:1.
我可以使用 xpath 吗
simplexml_load_file 而不是
简单的xml元素? 2.我可以更换吗
使用变量值 Arnold Arichson
像 $users_firstname 和
$users_lastname=

我不是 PHP 人员,但我相信您应该能够执行类似的操作(您可能需要以不同的方式连接 XPATH 字符串,或者可能不需要转义单引号,不确定):

<?php

$first = "Arnold";
$last = "Arichson";

$userInfo =  simplexml_load_file('http://your.website.com/user_information.xml');

foreach ($userInfo->xpath("/user_information/user[name=\'$first\' and lastname/name=\'$last\']/@url") as $url) {
  echo "<h2>" . $url . "</h2>";
}
?>

This XPATH selects the @url for Arnold Archison:

/user_information/user[name='Arnold' and lastname/name='Arichson']/@url

EDIT

Two questions about your solution: 1.
Can i use xpath with
simplexml_load_file instead of
simplexmlelement? 2. Can i replace the
values Arnold Arichson with a variable
like $users_firstname and
$users_lastname=

I'm not a PHP guy, but I believe that you should be able to do something like this (you might need to concatenate the XPATH string differently, or may not need to escape the single quotes, not sure):

<?php

$first = "Arnold";
$last = "Arichson";

$userInfo =  simplexml_load_file('http://your.website.com/user_information.xml');

foreach ($userInfo->xpath("/user_information/user[name=\'$first\' and lastname/name=\'$last\']/@url") as $url) {
  echo "<h2>" . $url . "</h2>";
}
?>
攒一口袋星星 2024-10-16 19:12:30
$xml = new SimpleXMLElement($xml_string);
foreach ($xml->user as $value) {
   if ($value->name == $x && $value->lastname->name == $y) {
      $attr = $value->attributes();
      echo $attr['url'];
   }
}

这会将 xml 字符串加载到一个简单的 xml 对象中,然后比较节点值,如果它们与您所需的名称($x$y)匹配,则打印 url 。如果需要,您还可以加载 xml 文件而不是字符串来创建 simplexml 对象。有关更多信息,请参阅 simplexml 文档。

添加

使用字符串(已测试且有效):

<?php
    $xml_input = <<<XML
<user_information> <user url="http://usersweb.com"> <name>Arnold</name> <lastname> <name>Arnoldson</name> </lastname> <age>42</age> </user> <user url="http://anotheruserweb.com"> <name>Arnold</name> <lastname> <name>Arichson</name> </lastname> <age>42</age> </user></user_information>
XML;

$xml = new SimpleXMLElement($xml_input); 
foreach ($xml->user as $value) {
    if ($value->name == 'Arnold' && $value->lastname->name == 'Arnoldson') {
        $attr = $value->attributes();
        echo $attr['url'];
    }
} 
?>

使用文件(应该工作完全相同):

<?php
    $xml = new SimpleXMLElement($xml_file_path, 0, true); 
    foreach ($xml->user as $value) {
        if ($value->name == 'Arnold' && $value->lastname->name == 'Arnoldson') {
            $attr = $value->attributes();
            echo $attr['url'];
        }
    } 
?>

在对象构造中,我添加了参数 0 和 true,这允许您使用文件路径作为第一个参数而不是字符串。但是,是的,您也可以使用加载文件功能。

$xml = new SimpleXMLElement($xml_string);
foreach ($xml->user as $value) {
   if ($value->name == $x && $value->lastname->name == $y) {
      $attr = $value->attributes();
      echo $attr['url'];
   }
}

This will load the xml string into a simple xml object, then compare the node values, and if they match your desired name ($x and $y), print the url. You can also load an xml file instead of string to create the simplexml object if needed. see simplexml documentation for more info.

ADDITION

Using string (tested and works):

<?php
    $xml_input = <<<XML
<user_information> <user url="http://usersweb.com"> <name>Arnold</name> <lastname> <name>Arnoldson</name> </lastname> <age>42</age> </user> <user url="http://anotheruserweb.com"> <name>Arnold</name> <lastname> <name>Arichson</name> </lastname> <age>42</age> </user></user_information>
XML;

$xml = new SimpleXMLElement($xml_input); 
foreach ($xml->user as $value) {
    if ($value->name == 'Arnold' && $value->lastname->name == 'Arnoldson') {
        $attr = $value->attributes();
        echo $attr['url'];
    }
} 
?>

Using file (should work exact same):

<?php
    $xml = new SimpleXMLElement($xml_file_path, 0, true); 
    foreach ($xml->user as $value) {
        if ($value->name == 'Arnold' && $value->lastname->name == 'Arnoldson') {
            $attr = $value->attributes();
            echo $attr['url'];
        }
    } 
?>

In the object construct, I added the arguments 0 and true, which allows you to use a file path for the first argument instead of a string. But yes, you could also use the load file function as well.

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