SimpleXML

SimpleXMLElement

SimpleXMLElement::__construct

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>Here is some text.</body>
          </document>
          XML;
var_dump(new SimpleXMLElement($xml)); // object(SimpleXMLElement)#1 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }

SimpleXMLElement::addAttribute

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>Here is some text.</body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
$xml->addAttribute('foo', 'bar');
var_dump($xml); // object(SimpleXMLElement)#1 (3) { ["@attributes"]=> array(1) { ["foo"]=> string(3) "bar" } ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }

SimpleXMLElement::addChild

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->addChild('body', 'Here is some text.')); // object(SimpleXMLElement)#2 (1) { [0]=> string(18) "Here is some text." }
var_dump($xml);                                         // object(SimpleXMLElement)#1 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }

SimpleXMLElement::asXML

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>Here is some text.</body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
echo $xml->asXML();

SimpleXMLElement::attributes

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document foo="bar">
              <title>XML</title>
              <body>Here is some text.</body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->attributes()); // object(SimpleXMLElement)#2 (1) { ["@attributes"]=> array(1) { ["foo"]=> string(3) "bar" } }

SimpleXMLElement::children

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->children());                   // object(SimpleXMLElement)#2 (2) { ["title"]=> string(3) "XML" ["body"]=> object(SimpleXMLElement)#4 (1) { ["p"]=> string(18) "Here is some text." } }
var_dump($xml->children()->body->children()); // object(SimpleXMLElement)#4 (1) { ["p"]=> string(18) "Here is some text." }

SimpleXMLElement::count

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->count()); // int(2)

SimpleXMLElement::getDocNamespaces

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document xmlns:title="http://www.example.com/title" xmlns:body="http://www.example.com/body" xmlns:p="http://www.example.com/p">
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->getDocNamespaces()); // array(3) { ["title"]=> string(28) "http://www.example.com/title" ["body"]=> string(27) "http://www.example.com/body" ["p"]=> string(24) "http://www.example.com/p" }

SimpleXMLElement::getName

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
foreach ($xml->children() as $children) {
    var_dump($children->getName());
}
// string(5) "title"
// string(4) "body"

SimpleXMLElement::getNamespaces

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document xmlns:title="http://www.example.com/title" xmlns:body="http://www.example.com/body" xmlns:p="http://www.example.com/p">
              <title:title>XML</title:title>
              <body:body>
                  <p>Here is some text.</p>
              </body:body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->getNamespaces(true)); // array(2) { ["title"]=> string(28) "http://www.example.com/title" ["body"]=> string(27) "http://www.example.com/body" }

SimpleXMLElement::registerXPathNamespace

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document xmlns:title="http://www.example.com/title">
              <title:title>XML</title:title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
$xml->registerXPathNamespace('title', 'http://www.example.com/title');
var_dump($xml->xpath('//title:title')); // array(1) { [0]=> object(SimpleXMLElement)#2 (1) { [0]=> string(3) "XML" } }

SimpleXMLElement::saveXML

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>Here is some text.</body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
echo $xml->saveXML();

SimpleXMLElement::__toString

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
echo $xml;
print $xml;

SimpleXMLElement::xpath

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->xpath('/document/body')); // array(1) { [0]=> object(SimpleXMLElement)#2 (1) { ["p"]=> string(18) "Here is some text." } }

SimpleXMLIterator

SimpleXMLIterator::current

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xmlIterator = new SimpleXMLIterator($xml);
var_dump($xmlIterator->current()); // NULL

$xmlIterator->rewind();
var_dump($xmlIterator->current()); // object(SimpleXMLIterator)#2 (1) { [0]=> string(3) "XML" }

SimpleXMLIterator::getChildren

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
var_dump($xmlIterator->getChildren()); // object(SimpleXMLIterator)#2 (1) { [0]=> string(3) "XML" }

$xmlIterator->next();
var_dump($xmlIterator->getChildren()); // object(SimpleXMLIterator)#2 (1) { ["p"]=> string(18) "Here is some text." }

$xmlIterator->next();
var_dump($xmlIterator->getChildren()); // NULL

SimpleXMLIterator::hasChildren

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
var_dump($xmlIterator->hasChildren()); // bool(false)

$xmlIterator->next();
var_dump($xmlIterator->hasChildren()); // bool(true)

SimpleXMLIterator::key

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xmlIterator = new SimpleXMLIterator($xml);
var_dump($xmlIterator->key()); // bool(false)

$xmlIterator->rewind();
var_dump($xmlIterator->key()); // string(5) "title"

$xmlIterator->next();
var_dump($xmlIterator->key()); // string(4) "body"

SimpleXMLIterator::next

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
$xmlIterator->next();
var_dump($xmlIterator->current()); // object(SimpleXMLIterator)#2 (1) { ["p"]=> string(18) "Here is some text." }

SimpleXMLIterator::rewind

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
var_dump($xmlIterator->current()); // object(SimpleXMLIterator)#2 (1) { [0]=> string(3) "XML" }

SimpleXMLIterator::valid

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>
                  <p>Here is some text.</p>
              </body>
          </document>
          XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
var_dump($xmlIterator->valid()); // bool(true)

$xmlIterator->next();
var_dump($xmlIterator->valid()); // bool(true)

$xmlIterator->next();
var_dump($xmlIterator->valid()); // bool(false)

SimpleXML

simplexml_import_dom

<?php

$dom = new DomDocument();
$dom->loadXML('<document><title>XML</title><body>Here is some text.</body></document>');
var_dump(simplexml_import_dom($dom)); // object(SimpleXMLElement)#2 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }

simplexml_load_file

<?php

var_dump(simplexml_load_file(__DIR__ . '/example.xml')); // object(SimpleXMLElement)#1 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }

上例中的 example.xml 文件内容:

<?xml version="1.0" encoding="UTF-8"?>
    <document>
        <title>XML</title>
        <body>Here is some text.</body>
</document>

simplexml_load_string

<?php

$xml = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          <document>
              <title>XML</title>
              <body>Here is some text.</body>
          </document>
          XML;
var_dump(simplexml_load_string($xml)); // object(SimpleXMLElement)#1 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }

results matching ""

    No results matching ""