SimpleXMLElement类对象的实例化

时间:2021-06-26 09:21:53

首先简单介绍一下SimpleXMLElement类

finalpublicSimpleXMLElement::__construct() (string$data [, int$options = 0 [,bool $data_is_url = false [,string$ns = "" [, bool $is_prefix = false ]]]] )

Creates a new SimpleXMLElement object

data

A well-formed XML string or the path or URL to an XML document ifdata_is_url isTRUE.

options

Optionally used to specify additional Libxml parameters.

data_is_url

By default, data_is_url is FALSE. Use TRUE to specify that data is a path or URL to an XML document instead ofstring data

1)第一种

<?
$xml=new SimpleXMLElement("demo01.xml",null,true);

2)第二种【传参是字符串】

<?php
$str = <<<xml
<student>
<name>zhangsan</name>
<age>30</age>
</student>
xml;

$xml=new SimpleXMLElement($str);

3)第三种【通过simplexml_load_file方法可以返回SimpleXMLElement类的对象,完成实例化】 

<?
$xml=simplexml_load_file("demo01.xml");