Home php教程 php手册 php XPath对XML文件查找及修改实现代码

php XPath对XML文件查找及修改实现代码

Jun 06, 2016 pm 08:38 PM
xml xpath

php XPath对XML文件查找及修改实现代码,需要的朋友可以参考下。

代码如下:
/*



11
content1
2009-10-11


title2
content2
2009-11-11


*/
if(!function_exists('l')) {
function l() {
echo '
***********************************
';
}
}
if(file_exists('test10_12.xml')) {
$xml = simplexml_load_file('test10_12.xml');

//利用xpath方法查找节点位置
$query = $xml->xpath('//title[@name="t1"]/ancestor::item');

var_dump($query);
l();

//测试在title节点下新增子节点new
$query[0]->title->addChild('new','new');

//测试在title节点下新增属性fuck
$query[0]->title->addAttribute('fuck','fuck you');

//测试修改content节点值为hello,world
$query[0]->content = 'hello,world';

//不解释,你懂的
$query[0]->asXML("10.xml");
}
?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can I open an XML file using PowerPoint?

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Filtering and sorting XML data using Python

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data to CSV format in Python

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling errors and exceptions in XML using Python

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Import XML data into database using PHP

Use PHP and XML to process and display geolocation and map data Use PHP and XML to process and display geolocation and map data Aug 01, 2023 am 08:37 AM

Use PHP and XML to process and display geolocation and map data

See all articles