如何在PHP中使用属性从xml中获取数据?

如何在PHP中使用属性从xml中获取数据?,php,xml,xmlreader,Php,Xml,Xmlreader,我有一个具有以下模式的XML文件: <?xml version="1.0" encoding="UTF-8"?> <languages> <language type="persian" abbr_type="fa"> <menu> <home></home> <contact></contact> <about></about

我有一个具有以下模式的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<languages>
<language type="persian" abbr_type="fa">
    <menu>
        <home></home>
        <contact></contact>
        <about></about>
        <style></style>
    </menu>
    <title>
        <home></home>
        <contact></contact>
        <about></about>
        <style></style>
        <list></list>
        <biography></biography>
        <picture></picture>
        <movie></movie>
    </title>
    <about></about>
    <welcome></welcome>
</language>
<language type="english" abbr_type="en">
    <menu>
        <home></home>
        <contact></contact>
        <about></about>
        <style></style>
    </menu>
    <title>
        <home></home>
        <contact></contact>
        <about></about>
        <style></style>
        <list></list>
        <biography></biography>
        <picture></picture>
        <movie></movie>
    </title>
    <about></about>
    <welcome></welcome>
</language>

如果语言标签的属性是“波斯语”,我想得到标题数据

确切地说,如何从XML中获取一系列数据?有没有办法获取数据并放入数组

有没有办法获取数据并放入数组

是的,您可以使用DOMDocument>。它创建了树状结构,您可以在其中轻松找到您要找的内容

范例

$xml = new DOMDocument();
$xml->loadXML($xmlString);
$xmlNodeArray = $xml->getElementsByTagName('language');
foreach ($xmlNodeArray as $element) {
    if($element->getAttribute('type') == "persian") {
         // do something with that element
    }
}
|可能重复的