在PHP中读取xml属性

时间:2012-10-30 11:03:44

标签: php xml

我的XML文件包含以下代码:

<Object>
  <hasOLE>false</hasOLE> 
  <Attribute Name="Absolute Number">
    <Value>1</Value> 
  </Attribute>
  <Attribute Name="Object Number">
    <Value>1</Value> 
  </Attribute>
  <Attribute Name="Object Heading">
    <Value>Introduction</Value> 
  </Attribute>
  <Attribute Name="Object Text">
    <Value /> 
  </Attribute>
</Object>

如何阅读PHP中的属性?

1 个答案:

答案 0 :(得分:0)

使用simplexml_load_string

$string = '<Object>
    <hasOLE>false</hasOLE> 
    <Attribute Name="Absolute Number">
        <Value>1</Value> 
    </Attribute>
    <Attribute Name="Object Number">
        <Value>1</Value> 
    </Attribute>
    <Attribute Name="Object Heading">
        <Value>Introduction</Value> 
    </Attribute>
    <Attribute Name="Object Text">
        <Value /> 
    </Attribute>
</Object>';

$xml = simplexml_load_string( $string );
// Or $xml = simplexml_load_file( $filename ); if it's inside a file.

并像这样简单地访问它:

echo $xml->Attribute->attributes(); //Absolute Number 
echo $xml->Attribute->Value;        // 1