没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
PHP 操作 xml
SimpleXMLElement...........................................................................................................................2
1.保存为 XML 文件或字符串...................................................................................................2
2.读取节点(NODE)的属性:...............................................................................................2
3.查找指定节点的直接子节点:..............................................................................................2
4.读取一个 XML file,建立一个 simplexml 对象...................................................................4
5. 查询 xml 文件........................................................................................................................5
DOM Functions...................................................................................................................................6
1.建立一个 DOMDocument 对象..............................................................................................6
1) DOMDocument Extends DOMNode. 构造函数...........................................................6
3). bool DOMDocument->loadXML ( string source [, int options] )................................6
2.保存一个 DOMDocument 对象..............................................................................................6
1). mixed DOMDocument->save ( string filename [, integer options] )............................6
2). string DOMDocument-> saveXML ( [DOMNode node [, integer options]] ).............7
3.检查一个 DOMDocument 对象某些有效性..........................................................................7
2).bool DOMDocument->schemaValidate ( string filename ).........................................8
3)bool DOMDocument->schemaValidateSource ( string source )..................................8
2)DOMCDATASection CDATASection 对象表示文档中的 CDATA 区段....................9
3) DOMComment Extends DOMCharacterData................................................................9
4)DOMDocumentFragment................................................................................................9
5) DOMElement..................................................................................................................9
6)DOMEntityReference Extends DOMNode...................................................................10
7)DOMProcessingInstruction...........................................................................................10
8) DOMText Extends DOMCharacterData.......................................................................11
5.DOMDocument 对象 内容读取,编辑操作....................................................................11
2. DOMCharacterData Extends DOMNode.....................................................................11
3.DOMComment Extends DOMCharacterData...............................................................12
4. DOMDocument Extends DOMNode............................................................................12
5.DOMDocumentType Extends DOMNode.....................................................................15
6. DOMElement Extends DOMNode...............................................................................15
7.DOMEntity Extends DOMNode....................................................................................16
8.DOMEntityReference Extends DOMNode...................................................................17
9. DOMException.............................................................................................................17
10.DOMImplementation...................................................................................................17
11. DOMNode...................................................................................................................17
12. DOMNodeList.............................................................................................................19
13.DOMNotation Extends DOMNode.............................................................................20
14. DOMProcessingInstruction Extends DOMNode.......................................................20
15. DOMText Extends DOMCharacterData.....................................................................20
16. DOMXPath.................................................................................................................21
18.DocumentFragment 对象.............................................................................................24
预定义常量...............................................................................................................................24
SimpleXMLElement
1.保存为 XML 文件或字符串
mixed SimpleXMLElement->asXML ( [string filename] )
The asXML method formats the parent object's data in XML version 1.0.
参数
filename
If specified, the function writes the data to the file rather than returning it.
返回值
If the filename isn't specified, this function returns a string on success and FALSE on
error. If the parameter is specified, it returns TRUE if the file was written successfully
and FALSE otherwise.
2.读取节点(NODE)的属性:
SimpleXMLElement->attributes -- Identifies an element's attributes
Description
SimpleXMLElement simplexml_element->attributes ( [string data] )
This function provides the attributes and values defined within an xml tag.
返回值: array
注: SimpleXML 创建了一条给大部分方法增加交互式属性的规则。这些属性不
能够使用 var_dump() 或其它检查对象的方法来查看。
例子 1. Interpret an XML string
<?php
$string = <<<XML
<a>
<foo name="one" game="lonely">1</foo>
</a>
XML;
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
This script will display:
name="one"
game="lonely"
3.查找指定节点的直接子节点:
SimpleXMLElement->children
SimpleXMLElement->children -- Finds children of given node
Description
SimpleXMLElement simplexml_element->children ( [string nsprefix] )
This method finds the children of the element of which it is a member. The result
follows normal iteration rules.
返回值: array
simplexml_load_file
(PHP 5)
访问具体节点的具体某个属性值, 参照下例:
<?php
$xml = simplexml_load_string(
'<person>
<child role="son">
<child role="daughter"/>
</child>
<child role="daughter">
<child role="son">
<child role="son"/>
</child>
</child>
</person>');
foreach ($xml->children() as $second_gen) {
JJJJecho ' The person begot a ' . $second_gen['role'];
JJJJforeach ($second_gen->children() as $third_gen) {
JJJJJJJJecho ' who begot a ' . $third_gen['role'] . ';';
JJJJ
JJJJJJJJforeach ($third_gen->children() as $fourth_gen) {
JJJJJJJJJJJJecho ' and that ' . $third_gen['role'] .
JJJJJJJJJJJJJJJJ' begot a ' . $fourth_gen['role'];
JJJJJJJJ}
JJJJ}
}
?>
<?php
$xml_document =<<<EOT
<root xmlns:foo='http://google.com'>
<foo:bar>baz</foo:bar><foo:hh>yg</foo:hh ><root >
EOT;
$sxml_document = simplexml_load_string($xml_document);
$foo_ns_bar = $sxml_document->children('http://google.com');
// children('http://google.com') 指明属于哪个命名空间 的子节点
echo $foo_ns_bar[0]; // prints 'baz'
?>
-------------------------------
<?php
$xml_document =<<<EOT
<root >
<category>
<item>text</item>
<bold>text</bold>
<item>text</item>
<item>text</item>
<mark>text</mark>
<bold>text</bold>
</category></root>
EOT;
$sxml_document = simplexml_load_string($xml_document);
$category = $sxml_document->children();
foreach($category -> children() as $name => $node){
echo $name.'-'.$node'<br/>';
}
?>
输出:
Item-text<br/>bold-text<br/>item-text<br/>item-text<br/>mark-text<br/>bold-text
注意这两个方式下如何取得节点值。
4.读取一个 XML file,建立一个 simplexml 对象
simplexml_load_file -- Interprets an XML file into an object
Description
object simplexml_load_file ( string filename [, string class_name [, int options]] )
This function will convert the well-formed XML document in the file specified by
filename to an object of class SimpleXMLElement. If any errors occur during file
access or interpretation, the function returns FALSE.
You may use the optional class_name parameter so that simplexml_load_file() will
return an object of the specified class. That class should extend the
SimpleXMLElement class.
Since PHP 5.1.0 and Libxml 2.6.0, you may also use the options parameter to specify
additional Libxml parameters.
注: Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter
a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' .
urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it
for you.
----------------
simplexml_load_string 同前条规则, 区别只是读取的是一条 well-formed
XML 格式的 string
(PHP 5)
simplexml_load_string -- Interprets a string of XML into an object
---------------------------------------------------
simplexml_import_dom 同前条规则, 区别只是 Get a SimpleXMLElement
object from a DOM node
(PHP 5)
simplexml_import_dom -- Get a SimpleXMLElement object from a DOM node.
Description
SimpleXMLElement simplexml_import_dom ( DOMNode node [, string class_name]
)
This function takes a node of a DOM document and makes it into a SimpleXML
node. This new object can then be used as a native SimpleXML element. If any errors
occur, it returns FALSE.
5. 查询 xml 文件
SimpleXMLElement->xpath
(no version information, might be only in CVS)
SimpleXMLElement->xpath -- Runs Xpath query on XML data
Description
array SimpleXMLElement->xpath ( string path )
The xpath method searches the SimpleXML node for children matching the Xpath
path. It always returns an array of SimpleXMLElement objects.
XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历
XPath 路径表达式
XPath 使用路径表达式来选取 XML 文档中的节点或者节点集。这些路径表达式
和我们在常规的电脑文件系统中看到的表达式非常相似。
XPath 标准函数
XPath 含有超过 100 个内建的函数。这些函数用于字符串值、数值、日期和时
间比较、节点和 QName 处理、序列处理、逻辑值等等。
剩余25页未读,继续阅读
资源评论
- rink002013-01-15用上了,还能用
tyds715
- 粉丝: 1
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功