根据子内容解析 xml 属性
我加载一个包含用户元数据的外部 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此 XPATH 选择 Arnold Archison 的
@url
:编辑
我不是 PHP 人员,但我相信您应该能够执行类似的操作(您可能需要以不同的方式连接 XPATH 字符串,或者可能不需要转义单引号,不确定):
This XPATH selects the
@url
for Arnold Archison:EDIT
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):
这会将 xml 字符串加载到一个简单的 xml 对象中,然后比较节点值,如果它们与您所需的名称(
$x
和$y
)匹配,则打印 url 。如果需要,您还可以加载 xml 文件而不是字符串来创建simplexml
对象。有关更多信息,请参阅simplexml
文档。添加
使用字符串(已测试且有效):
使用文件(应该工作完全相同):
在对象构造中,我添加了参数 0 和 true,这允许您使用文件路径作为第一个参数而不是字符串。但是,是的,您也可以使用加载文件功能。
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 thesimplexml
object if needed. seesimplexml
documentation for more info.ADDITION
Using string (tested and works):
Using file (should work exact same):
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.