Php 简单XML元素:获取内部href<;链接rel=";候补&燃气轮机;节点
我试图解析xml文件中的不同链接。我阅读了文档和我找到的每一篇关于解析xml文件的帖子,但是我没有找到一种我想要的访问节点的方法。例如:Php 简单XML元素:获取内部href<;链接rel=";候补&燃气轮机;节点,php,xml,simplexml,Php,Xml,Simplexml,我试图解析xml文件中的不同链接。我阅读了文档和我找到的每一篇关于解析xml文件的帖子,但是我没有找到一种我想要的访问节点的方法。例如: <link rel="self" type="text/html" title="title0" length="8359" href="http://example0.com"/> <link rel="alternate" type="text/html" title="title1" length="8359" href="http:/
<link rel="self" type="text/html" title="title0" length="8359" href="http://example0.com"/>
<link rel="alternate" type="text/html" title="title1" length="8359" href="http://example3.com"/>
<link rel="related" type="text/html" title="title2" length="8359" href="http://example4.com"/>
<link rel="related" type="text/html" title="title3" length="8359" href="http://example4.com"/>
<link rel="related" type="text/html" title="title4" length="8359" href="http://example5.com"/>
<link rel="related" type="text/html" title="title5" length="8359" href="http://example5.com"/>
…如果您处理的是大文件,最好将文件分成几行,然后使用preg_match处理每一行。如果您的XML文件具有类似的结构,这显然是最有效的。例如,您可以使用if/switch语句
foreach($xml->getElementsByTagName('link') as $tag) {
switch($tag->getAttribute('rel')) {
case 'self':
$href_of_self = $tag->getAttribute('href');
break;
case 'related':
...
}
}
通过标记获取元素和获取元素属性可以通过以下方法完成:
您通常希望使用或类似它来解析类似XML的文档。SimpleXML支持它。例如:
<?php
$string = <<<XML
<div>
<link rel="self" type="text/html" title="title0" length="8359" href="http://example0.com"/>
<link rel="alternate" type="text/html" title="title1" length="8359" href="http://example3.com"/>
<link rel="related" type="text/html" title="title2" length="8359" href="http://example4.com"/>
<link rel="related" type="text/html" title="title3" length="8359" href="http://example4.com"/>
<link rel="related" type="text/html" title="title4" length="8359" href="http://example5.com"/>
<link rel="related" type="text/html" title="title5" length="8359" href="http://example5.com"/>
</div>
XML;
$xml = new SimpleXMLElement($string);
foreach(['self', 'alternate', 'related', 'dne'] as $rel) {
$val = @$xml->xpath("//link[@rel='$rel']/@href");
$val = $val ? array_map(function($n) { return (string)$n; }, $val) : [];
$val = count($val) == 1 ? $val[0] : $val;
var_dump($val);
}
xpath(//link[@rel='$rel']/@href”);
$val=$val?数组映射(函数($n){return(string)$n;},$val):[];
$val=计数($val)==1$val[0]:$val;
var_dump($val);
}
如果您不习惯使用xpath,那么可以像访问对象一样访问link元素:
<?php
$string = <<<XML
<div>
<link rel="self" type="text/html" title="title0" length="8359" href="http://example0.com"/>
<link rel="alternate" type="text/html" title="title1" length="8359" href="http://example3.com"/>
<link rel="related" type="text/html" title="title2" length="8359" href="http://example4.com"/>
<link rel="related" type="text/html" title="title3" length="8359" href="http://example4.com"/>
<link rel="related" type="text/html" title="title4" length="8359" href="http://example5.com"/>
<link rel="related" type="text/html" title="title5" length="8359" href="http://example5.com"/>
</div>
XML;
$xml = new SimpleXMLElement($string);
$related = [];
foreach($xml->link as $link) {
switch($link['rel']){
case 'self':
$self = $link['href'];
break;
case 'alternate':
$alternate = $link['href'];
break;
case 'related':
array_push($related, $link['href']);
break;
}
}
print $self;
// outputs : http://example0.com
print $alternate;
// outputs : http://example3.com
print_r($related);
/* outputs : Array
(
[0] => SimpleXMLElement Object
(
[0] => http://example4.com
)
[1] => SimpleXMLElement Object
(
[0] => http://example4.com
)
[2] => SimpleXMLElement Object
(
[0] => http://example5.com
)
[3] => SimpleXMLElement Object
(
[0] => http://example5.com
)
)
*/
您可以将其本身描述为“一个您可能并不讨厌的PHP XML库”。请注意函数parseCurrentElement()
您可以创建自定义读取器
class CustomXmlReader extends \Sabre\Xml\Reader {}
class CustomXmlService extends \Sabre\Xml\Service {}
这个问题可以概括为“如何基于XML元素的一个其他属性的值来访问XML元素的属性”。有两种基本方法:迭代所有候选元素,并检查属性值;或者使用XPath搜索文档 一旦找到匹配的元素,就需要访问该属性,在SimpleXML中,这意味着了解两种语法:
从表示元素的对象(例如$something['bar']
)到表示其属性之一的对象(例如
)bar=“…”
将变量强制转换为字符串,对于SimpleXML,它为您提供元素或属性的完整字符串内容(string)$something
foreach
和if
。假设$xml
已经指向
元素的父元素:
foreach ( $xml->link as $link ) {
if ( $link['rel'] == 'self' ) {
// Found <link rel="self">
// assign to variable, return from function, etc
// To access the attribute, we use $link['href']
// To get the text content of the selected node,
// we cast to string with (string)$link['href']
$self_link = (string)$link['href'];
}
}
虽然这个库可能为这个任务提供了一个有趣的解决方案,但您的答案并没有真正解释如何做到这一点。因此,我不确定它是否比SimpleXML更有用,他们已经发现了SimpleXML。很好的解释,您包括了这两种方法(使用SimpleXML和xpath),并且有很好的文档记录。
class CustomXmlReader extends \Sabre\Xml\Reader {}
class CustomXmlService extends \Sabre\Xml\Service {}
foreach ( $xml->link as $link ) {
if ( $link['rel'] == 'self' ) {
// Found <link rel="self">
// assign to variable, return from function, etc
// To access the attribute, we use $link['href']
// To get the text content of the selected node,
// we cast to string with (string)$link['href']
$self_link = (string)$link['href'];
}
}
$xpath_results = $xml->xpath('//link[@rel="self"]');
foreach ( $xpath_results as $node ) {
// Again, we have a SimpleXMLElement object, and want
// the string content of the 'href' attribute:
$self_link = (string)$node['href'];
}