Viewed   59 times

I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<gnm:Workbook xmlns:gnm="" xmlns:xsi="" xsi:schemaLocation="">
  <office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="" xmlns:dc="" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:ooo="" office:version="1.1">
      <dc:creator>Mark Baker</dc:creator>
      <meta:generator>$Win32 OpenOffice.org_project/310m11$Build-9399</meta:generator>

And am trying to read the office:document-meta node to extractthe various elements below it (dc:creator, meta:creation-date, etc.)

The following code:

$xml = simplexml_load_string($gFileData);
$namespacesMeta = $xml->getNamespaces(true);
$officeXML = $xml->children($namespacesMeta['office']);
echo '<hr />';

gives me:

  public 'document-meta' => 
      public '@attributes' => 
          'version' => string '1.1' (length=3)
      public 'meta' => 

but if I try to read the document-meta element using:

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

I get

Notice: Use of undefined constant meta - assumed 'meta' in /usr/local/apache/htdocsNewDev/PHPExcel/Classes/PHPExcel/Reader/Gnumeric.php on line 273
int 0

I assume that SimpleXML is trying to extract a non-existent node "document" from $officeXML, then subtract the value of (non-existent) constant "meta", resulting in forcing the integer 0 result rather than the document-meta node.

Is there a way to resolve this using SimpleXML, or will I be forced to rewrite using XMLReader? Any help appreciated.



Your assumption is correct. Use


to make it work.

Please note that the above applies to Element nodes. Attribute nodes (those within the @attributes property when dumping the SimpleXmlElement) do not require any special syntax to be accessed when hyphenated. They are regularly accessible via array notation, e.g.

$xml = <<< XML
    <hyphenated-element hyphenated-attribute="bar">foo</hyphenated-element>
$root = new SimpleXMLElement($xml);
echo $root->{'hyphenated-element'}; // prints "foo"
echo $root->{'hyphenated-element'}['hyphenated-attribute']; // prints "bar"

See the SimpleXml Basics in the Manual for further examples.

Friday, November 18, 2022

You need to use the namespace when getting the attribute. The android namespace is defined as:

So you need to pass that to the attributes() method, like this:

$xml = simplexml_load_string($xmlStr);

echo (string) $xml->application->activity->attributes('')->name;



Codepad Demo

Saturday, September 17, 2022

The elements you're asking about are not part of the default namespace but in a different one. You can see that because they have a prefix in their name separated by the colon:

    <!-- title is in the default namespace, no colon in the name -->
    <g:price>199.00 EUR</g:price>
    <!-- price and gtin are in a different namespace, colon in the name and prefixed by "g" -->

The namespace is given with a prefix, here "g" in your case. And the prefix the namespace stands for is defined in the document element here:

<rss xmlns:g="" version="2.0">

So the namespace is "".

When you access the child-elements by their name with the SimpleXMLElement as you currently do:

$a = $node->title;
$b = $node->price;
$c = $node->gtin;

you're looking only in the default namespace. So only the first element actually contains text, the other two are created on-thy-fly and are yet empty.

To access the namespaced child-elements you need to tell the SimpleXMLElement explicitly with the children() method. It creates a new SimpleXMLElement with all the children in that namespace instead of the default one:

$google = $node->children("");

$a = $node->title;
$b = $google->price;
$c = $google->gtin;

So much for the isolated example (yes, that's it already).

A full example then could look like (including node-expansion on the reader, the code you had was a bit rusty):

 * How to parse an XML node with a colon tag using PHP
 * @link http://.com/q/29876898/367456
const HTTP_BASE_GOOGLE_COM_NS_1_0 = "";

$url = '';

$reader = new XMLReader;

$doc = new DOMDocument;

// move to first item element
while (($valid = $reader->read()) && $reader->name !== 'item') ;

while ($valid) {
    $default    = simplexml_import_dom($reader->expand($doc));
    $googleBase = $default->children(HTTP_BASE_GOOGLE_COM_NS_1_0);
        "%s - %s - %s<br />n"
        , htmlspecialchars($default->title)
        , htmlspecialchars($googleBase->price)
        , htmlspecialchars($googleBase->gtin)

    // move to next item element
    $valid = $reader->next('item');

I hope this both gives an explanation and broadens the view a little on XMLReader use as well.

Sunday, November 13, 2022

Use xpath():

$xml = simplexml_load_string($x); // assume XML in $x
$changes = $xml->xpath("//*[starts-with(local-name(), 'change')]");

This will select all nodes starting with change. The // will select them from whatever position in the tree. The results are stored as SimpleXML elements in an array in $changes.

Now change the selected nodes:

foreach ($changes as $change) 
    $change[0] = "New Text";

Take a look at the changed XML:

echo $xml->asXML();

see it working:

Friday, August 19, 2022

I had the same problem and I'm using Ubuntu 15.10.

In my case, to solve this issue, I installed the package php7.0-xml using the Synaptic package manager, which include SimpleXml. So, after restart my Apache server, my problem was solved. This package came in the Debian version and you can find it here:

Thursday, September 1, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :