使用PHP从XML获取标记值

时间:2022-06-01 20:38:19

My XML file looks like

我的XML文件看起来像

<entry>
    some tags 
    <title type='text'> content to be extracted </title>
    <content type='text'> content to be extracted </content>
    <link rel='alternate' type='text/html' href='https://www.youtube.com/watch?v=content to be extracted &amp;feature=youtube_gdata'/>
</entry>

to achieve this if I use the following ...

如果我使用以下内容来实现这一点......

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadXML("kFeed.xml");
$entryTag = $dom->getElementsByTagName("entry")->item(0)->nodeValue;
$title = $dom->$entryTag->getElementsByTagName("title")->item(0)->nodeValue;
$descr = $dom->$entryTag->getElementsByTagName("content")->item(0)->nodeValue;

It is throwing the following error like Notice: Trying to get property of non-object for 4th line and Fatal error: Cannot access empty property for 5th line.

它抛出以下错误,如注意:尝试获取第4行的非对象属性和致命错误:无法访问第5行的空属性。

Can someone please suggest me how to crack this and how to extract the 'v' value from the href of link (I have 4 links and I want that value which has rel='alternate').

有人可以建议我如何破解这个以及如何从链接的href中提取'v'值(我有4个链接,我想要那个具有rel ='alternate'的值)。

And finally when I directly give an URL (instead kFeed.xml) to load XML it is not functioning, no error.

最后,当我直接给出一个URL(而不是kFeed.xml)来加载XML时,它没有运行,没有错误。

4 个解决方案

#1


1  

loadxml($xml) expects $xml to be the xml data itself, not a pointer to a (file) source.
Use DOMDocument::load($filename) instead.

loadxml($ xml)期望$ xml是xml数据本身,而不是指向(文件)源的指针。请改用DOMDocument :: load($ filename)。

And $dom->$entryTag->... won't work either. You might be interested in DOMXPath or SimpleXML instead.

并且$ dom - > $ entryTag - > ...也不起作用。您可能对DOMXPath或SimpleXML感兴趣。

#2


1  

This will work for you

这对你有用

$str    =   "<entry>
some tags 
<title type='text'> content to be extracted </title>
<content type='text'> content to be extracted </content>
<link rel='alternate' type='text/html' href='https://www.youtube.com/watch?v=content to be extracted &amp;feature=youtube_gdata'/>
</entry>";

$xml = simplexml_load_string($str);

echo $xml->title;    //Will print title

echo "<pre>";
print_r($xml->link->attributes());   //Will print attributes also

#3


0  

Should you actually be accessing the item as an array rather than as an object?

你真的应该作为一个数组而不是一个对象访问该项目吗?

$descr = $dom->$entryTag->getElementsByTagName("content")->item[0]->nodeValue;

#4


0  

$xml = simplexml_load_file('xx.xml');

$title = (string)$xml->title;
$content = (string)$xml->content;
$linkHref = (string)$xml->link['href'];

$url = parse_url($linkHref);
parse_str($url['query'], $params);

echo '<pre>'; print_r($params['v']); echo '</pre>';

#1


1  

loadxml($xml) expects $xml to be the xml data itself, not a pointer to a (file) source.
Use DOMDocument::load($filename) instead.

loadxml($ xml)期望$ xml是xml数据本身,而不是指向(文件)源的指针。请改用DOMDocument :: load($ filename)。

And $dom->$entryTag->... won't work either. You might be interested in DOMXPath or SimpleXML instead.

并且$ dom - > $ entryTag - > ...也不起作用。您可能对DOMXPath或SimpleXML感兴趣。

#2


1  

This will work for you

这对你有用

$str    =   "<entry>
some tags 
<title type='text'> content to be extracted </title>
<content type='text'> content to be extracted </content>
<link rel='alternate' type='text/html' href='https://www.youtube.com/watch?v=content to be extracted &amp;feature=youtube_gdata'/>
</entry>";

$xml = simplexml_load_string($str);

echo $xml->title;    //Will print title

echo "<pre>";
print_r($xml->link->attributes());   //Will print attributes also

#3


0  

Should you actually be accessing the item as an array rather than as an object?

你真的应该作为一个数组而不是一个对象访问该项目吗?

$descr = $dom->$entryTag->getElementsByTagName("content")->item[0]->nodeValue;

#4


0  

$xml = simplexml_load_file('xx.xml');

$title = (string)$xml->title;
$content = (string)$xml->content;
$linkHref = (string)$xml->link['href'];

$url = parse_url($linkHref);
parse_str($url['query'], $params);

echo '<pre>'; print_r($params['v']); echo '</pre>';