在PHP中使用simplexml_load_string解析XML

时间:2016-12-16 07:53:50

标签: php xml dom simplexml

<info>
    <form tableid="1">
        <town_id>
            <option value="5102">Moscow</option>
            <option value="2587">London</option>
            <option value="717">Madrid</option>
            <option value="2513">Paris</option>
            <option value="5071">Berlin</option>
        </town_id>
    </form>
</info>

我有这样的xml并想通过simplexml_load_string解析它。函数返回这样的对象:

SimpleXMLElement Object
(
    [form] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [tableid] => 1
                )

            [town_id] => SimpleXMLElement Object
                (
                    [option] => Array
                        (
                            [0] => Moscow
                            [1] => London
                            [2] => Madrid
                            [3] => Paris
                            [4] => Berlin
                        )

                )

        )

)

我没有城镇选择的第二属性价值。我怎么能得到它们? 我的代码:

/** @var SimpleXMLElement $xml */
$xml = simplexml_load_string($data);
if (! is_object($xml)) return FALSE;

print_r($xml);
foreach($xml as $record){
    $attr = $record->attributes();
    $table_id = (int)$attr['tableid'];
   foreach($record as $key => $value){
      //$table_data[$table_id][$key][] = $value['option'];
       print_r($value->attributes());
       print_r($value['option']->attributes());
   }
}

$valueSimpleXMLElement个对象,但$value->attributes()$value['option]->attributes()返回空数组。

1 个答案:

答案 0 :(得分:1)

您可以通过使用SimpleXMLElement运算符访问子项来简单地迭代->个对象:

$xml = <<<'XML'
<info>
    <form tableid="1">
        <town_id>
            <option value="5102">Moscow</option>
            <option value="2587">London</option>
            <option value="717">Madrid</option>
            <option value="2513">Paris</option>
            <option value="5071">Berlin</option>
        </town_id>
        <town_id>
            <option value="9343">XTown</option>
        </town_id>
    </form>
</info>
XML;

$items = simplexml_load_string($xml);

foreach ($items as $form) {
    foreach ($form->town_id as $town) {
        foreach ($town->option as $option) {
            $attr = $option->attributes();
            printf("#%d - %s\n", $attr['value'], $option);
        }
    }
}

输出

#5102 - Moscow
#2587 - London
#717 - Madrid
#2513 - Paris
#5071 - Berlin
#9343 - XTown

的XPath

或者,使用xpath方法:

$options = $items->xpath('form/town_id/option');
foreach ($options as $option) {
  $attr = $option->attributes();
  printf("#%d - %s\n", $attr['value'], $option);
}

在这个例子中,我使用了相对于$items的XPath表达式(特别是根元素)。根据您的需要调整XPath。例如,您可以使用//option获取文档中的所有选项。或者您甚至可能希望迭代具有form子项的option下的所有元素:

$containers = $items->xpath('form/*[option]');
foreach ($containers as $c) {
  switch ($c->getName()) {
    case 'town_id':    $label = 'Towns';     break;
    case 'country_id': $label = 'Countries'; break;
    default:
      // Skipping unknown element name
      continue;
  }

  printf("\n%s\n======\n", $label);
  foreach ($c->option as $option) {
    $attr = $option->attributes();
    printf("#%d - %s\n", $attr['value'], $option);
  }
}

示例输出

Towns
======
#5102 - Moscow
#2587 - London
#717 - Madrid
#2513 - Paris
#5071 - Berlin

Towns
======
#9343 - XTown

Countries
======
#3456 - Russia
#4566 - China