相关疑难解决方法(0)

SimpleXML具有带连字符名称的读取节点

我有以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<gnm:Workbook xmlns:gnm="http://www.gnumeric.org/v10.dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gnumeric.org/v9.xsd">
  <office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:ooo="http://openoffice.org/2004/office" office:version="1.1">
    <office:meta>
      <dc:creator>Mark Baker</dc:creator>
      <dc:date>2010-09-01T22:49:33Z</dc:date>
      <meta:creation-date>2010-09-01T22:48:39Z</meta:creation-date>
      <meta:editing-cycles>4</meta:editing-cycles>
      <meta:editing-duration>PT00H04M20S</meta:editing-duration>
      <meta:generator>OpenOffice.org/3.1$Win32 OpenOffice.org_project/310m11$Build-9399</meta:generator>
    </office:meta>
  </office:document-meta>
</gnm:Workbook>
Run Code Online (Sandbox Code Playgroud)

我正在尝试阅读办公室:document-meta节点,以提取它下面的各种元素(dc:creator,meta:creation-date等)

以下代码:

$xml = simplexml_load_string($gFileData);
$namespacesMeta = $xml->getNamespaces(true);
$officeXML = $xml->children($namespacesMeta['office']);
var_dump($officeXML);
echo '<hr />';
Run Code Online (Sandbox Code Playgroud)

给我:

object(SimpleXMLElement)[91]
  public 'document-meta' => 
    object(SimpleXMLElement)[93]
      public '@attributes' => 
        array
          'version' => string '1.1' (length=3)
      public 'meta' => 
        object(SimpleXMLElement)[94]
Run Code Online (Sandbox Code Playgroud)

但如果我尝试使用以下方法读取document-meta元素:

$xml = simplexml_load_string($gFileData);
$namespacesMeta = $xml->getNamespaces(true);
$officeXML = $xml->children($namespacesMeta['office']);
$docMeta = $officeXML->document-meta;
var_dump($docMeta);
echo '<hr />';
Run Code Online (Sandbox Code Playgroud)

我明白了

Notice: …
Run Code Online (Sandbox Code Playgroud)

php simplexml

20
推荐指数
1
解决办法
1万
查看次数

解析错误:语法错误,意外T_OBJECT_OPERATOR

尝试将myXml.xml中的一些数据添加到字符串时,我收到以下错误:解析错误:语法错误,意外T_OBJECT_OPERATOR.

    $xmlstr = file_get_contents('myXml.xml');
    $xml = new SimpleXMLElement($xmlstr); 

    foreach($xml->order as $order){
            $replace = array();
            $firstName = (string) $order->billing-address->first-name;
            $lastName = (string) $order->billing-address->last-name;
    }
Run Code Online (Sandbox Code Playgroud)

我不能直接提供我的XML,因为它包含敏感数据.

谢谢,山姆

php xml simplexml

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

php ×2

simplexml ×2

xml ×1