我想过滤一个xml Feed,所以我只用正确的语言导入数据。
结构是:
<profile_lang lang="en">
<description>English description</description>
</profile_lang>
<profile_lang lang="fr">
<description>French</description>
</profile_lang>
<profile_lang lang="nl">
<description>Dutch text</description>
</profile_lang>
等
我需要做什么才能导入dutch(lang =“nl”)描述?
答案 0 :(得分:2)
尝试
$xml = <<<XML
<profile_lang lang="en">
<description>English description</description>
</profile_lang>
<profile_lang lang="fr">
<description>French</description>
</profile_lang>
<profile_lang lang="nl">
<description>Dutch text</description>
</profile_lang>
XML;
$xml = new SimpleXMLElement($xml);
$nodes = $xml->xpath('//*[@lang="en"]');
echo 'Found ', count($nodes), ' node(s) with lang "en".';
答案 1 :(得分:1)
xpath是一种在XML结构中定位/过滤/搜索特定元素的好方法
$xml = '<root><profile_lang lang="en"><description>English description</description></profile_lang><profile_lang lang="fr"><description>French</description></profile_lang><profile_lang lang="nl"><description>Dutch text</description></profile_lang></root>';
$simple = simplexml_load_string($xml);
$description = $simple->xpath('/root/profile_lang[@lang="nl"]/description');
echo $description[0]; // Dutch text
您可以提出自己的路径结构以满足您的需求
/root/profile_lang[@lang="nl"]/description
//profile_lang[@lang="nl"]/description
//profile_lang[@lang="nl"]
//description[../@lang="nl"]