Hello,
here goes my contribution for those whom are struggling to understand how SimpleXMLElement works.
After some time trying to figure out how this works, I've came up to this small example:
<?php
$xmlstr = "<?xml version='1.0' ?>\n".
// optionally you can specify a xml-stylesheet for presenting the results. just uncoment the following line and change the stylesheet name.
/* "<?xml-stylesheet type='text/xsl' href='xml_style.xsl' ?>\n". */
"<book></book>";
// create the SimpleXMLElement object with an empty <book> element
$xml = new SimpleXMLElement($xmlstr);
// add some child nodes
$xml->addChild("title", "Title of my book");
$xml->addChild("abstract", "My book is about learning to work with SimpleXMLElement");
// add some more child nodes
$chapter1 = $xml->addChild("chapter_1");
// add an attribute to child chapter_1
$chapter1->addAttribute("chapter_title", "Introduction to my book");
$chapter2 = $xml->addChild("chapter_2");
$chapter2->addAttribute("chapter_title", "Development of my book");
$chapter3 = $xml->addChild("chapter_3");
$chapter3->addAttribute("chapter_title", "Another chapter of my book");
$conclusion = $xml->addChild("conclusion", "The ending of my book");
// insert the header to tell the browser how to read the document
header("Content-type: text/xml");
// print the SimpleXMLElement as a XML well-formed string
echo $xml->asXML();
?>
With this script you can just copy-paste and try to understand how it works.
I hope it can help somebody :)
SimpleXMLElement クラス
導入
XML ドキュメントの要素をあらわします。
クラス概要
SimpleXMLElement
SimpleXMLElement
{
/* メソッド */
void addAttribute
( string $name
, string $value
[, string $namespace
] )
SimpleXMLElement addChild
( string $name
[, string $value
[, string $namespace
]] )
mixed asXML
([ string $filename
] )
SimpleXMLElement attributes
([ string $ns
[, bool $is_prefix
]] )
SimpleXMLElement children
([ string $ns
[, bool $is_prefix
]] )
array getDocNamespaces
([ bool $recursive
] )
string getName
( void
)
array getNamespaces
([ bool $recursive
] )
bool registerXPathNamespace
( string $prefix
, string $ns
)
array xpath
( string $path
)
}目次
- SimpleXMLElement::addAttribute — SimpleXML 要素に属性を追加する
- SimpleXMLElement::addChild — XML ノードに子要素を追加する
- SimpleXMLElement::asXML — SimpleXML 要素に基づき整形式の XML 文字列を返す
- SimpleXMLElement::attributes — 要素の属性を定義する
- SimpleXMLElement::children — 指定したノードの子ノードを見付ける
- SimpleXMLElement::__construct — 新しい SimpleXMLElement オブジェクトを作成する
- SimpleXMLElement::getDocNamespaces — ドキュメントで宣言されている名前空間を返す
- SimpleXMLElement::getName — XML 要素の名前を取得する
- SimpleXMLElement::getNamespaces — ドキュメントで使用している名前空間を返す
- SimpleXMLElement::registerXPathNamespace — 次の XPath クエリ用の prefix/ns コンテキストを作成する
- SimpleXMLElement::xpath — XML データに Xpath クエリを実行する
SimpleXMLElement
ivandosreisandrade at gmail dot com
20-Nov-2009 02:55
20-Nov-2009 02:55
brett at brettbrewer dot com
28-Oct-2009 12:53
28-Oct-2009 12:53
Figuring out how to access the properties of a SimpleXmlElement object was a little tricky for me. In particular, it took a while to discover that I needed to cast my SimpleXmlElement properties to be of type "string" to print them or do comparisons on them. For instance, assuming you already have a string of xml in $xmlstr...
<?php
$sxml= new SimpleXmlElement($xmlstr);
if ((string) $sxml->property== "somevalue") {
echo (string) $sxml->property;
}
?>
The properties of a SimpleXmlElement object are objects themselves, so you need to put "(string)" before them, which casts their values to a string instead of an object. I assume if you were doing a numeric comparison you'd want to cast to an (int) or something numeric instead.
