Asked  7 Months ago    Answers:  5   Viewed   29 times

I have the following 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>

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']);
var_dump($officeXML);
echo '<hr />';

gives me:

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

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;
var_dump($docMeta);
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.

 Answers

76

Your assumption is correct. Use

$officeXML->{'document-meta'}

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
<root>
    <hyphenated-element hyphenated-attribute="bar">foo</hyphenated-element>
</root>
XML;
$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.

Wednesday, March 31, 2021
 
Shibbir
answered 7 Months ago
93

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

http://schemas.android.com/apk/res/android

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

$xml = simplexml_load_string($xmlStr);

echo (string) $xml->application->activity->attributes('http://schemas.android.com/apk/res/android')->name;

Outputs

com.sunil.tweet.MainActivity

Codepad Demo

Wednesday, March 31, 2021
 
daniel__
answered 7 Months ago
38

All you need is

$data = new SimpleXMLElement($xml);
$data->registerXPathNamespace('ns1','http://endpoint.websitecom/');
$part = $data->xpath("//ns1:return");
var_dump($part[0]->children("ns1",true));

Output

object(SimpleXMLElement)[3]
  public 'campaignID' => string '0' (length=1)
  public 'categoryID' => string '200230455' (length=9)
  public 'categoryName' => string 'Promotion' (length=9)
  public 'linkID' => string '10001599' (length=8)
  public 'linkName' => string 'KFL-20% off No Min' (length=18)
  public 'mid' => string '3071' (length=4)
  public 'nid' => string '1' (length=1)
  public 'clickURL' => string '
            http://someurl
        ' (length=36)
  public 'endDate' => string 'Oct 15, 2012' (length=12)
  public 'height' => string '250' (length=3)
  public 'iconURL' => string '
            http://someurl
        ' (length=36)
  public 'imgURL' => string '
            http://someurl
        ' (length=36)
  public 'landURL' => string '
            http://someurl
        ' (length=36)
  public 'serverType' => string '22' (length=2)
  public 'showURL' => string '
            http://someurl
        ' (length=36)
  public 'size' => string '13' (length=2)
  public 'startDate' => string 'Oct 14, 2012' (length=12)
  public 'width' => string '300' (length=3)
Wednesday, March 31, 2021
 
rojo
answered 7 Months ago
46

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: https://eval.in/231427

Wednesday, March 31, 2021
 
Kemrop
answered 7 Months ago
44

The error is pretty descriptive dont you think?

http:// wrapper is disabled in the server configuration by allow_url_fopen=0

You will need to edit the PHP configuration file and change the configuration allow_url_fopen. If you cant do this directly try ini_set()

Edit: As @evanmcd pointed out in the comments, this configuration can only be set in php.ini. PHP documentation reference.

Saturday, May 29, 2021
 
WooDzu
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :