开发者

working with XML in PHP

I have a string exactly like that:

<info>
<m_album>value1</m_album>     
<m_cat>value2</m_cat>     
<cat_type/>     
<cat_permission>0</cat_permission>     
<m_img_thumb><!--[CDATA[urlvaluetoimage]]--></m_img_thumb>     
<m_img_medium><!--[CDATA[urlvaluetoimage]]--></m_img_m开发者_如何转开发edium>     
<m_img_large><!--[CDATA[urlvaluetoimage]]--></m_img_large>     
<m_hot>0</m_hot>     
<album_hot>0</album_hot>     
</info>

How do I get value from these tags like XML in PHP? Please help me and thank you for your attention!


You might want to look into the SimpleXML extension if you are using PHP5. Assuming the file above is loaded into a string called $xmlstr, you could access the value of m_album like this:

$xml = new SimpleXMLElement($xmlstr);
echo $xml->m_album;


<m_img_thumb><!--[CDATA[urlvaluetoimage]]--></m_img_thumb>

is a comment, not any form of text content. If you really want to read the content of a comment you could eg. load it into a DOMDocument and:

$c= $doc->getElementsByTagName('m_img_thumb')[0]->firstChild->data;
$c= $c.substr(7, -2); // remove [CDATA[ and ]] from sides of string

But this is probably a mistake. Most likely you meant to use a CDATA section:

<m_img_thumb><![CDATA[urlvaluetoimage]]></m_img_thumb>

although there's not really much point in using CDATA sections compared to just normal text content. (CDATA sections are often mistakenly used by authors who think it stops them having to worry about XML-injection issues. Top tip: No.)

Either way, you can get the textual content of the element using eg.

$c= $doc->getElementsByTagName('m_img_thumb')[0]->textContent;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜