


The following example simply demonstrates the operation of DOM on XML. For detailed explanation, please see the comments in the code
<? /************************************************ ** use XML in PHP5 ** reference site: ** http://cn.php.net/manual/zh/ref.dom.php ** the follow codes need PHP5 support *************************************************/ //首先要创建一个DOMDocument对象 $dom = new DomDocument(); //然后载入XML文件 $dom -> load("test.xml"); //输出XML文件 //header("Content-type: text/xml;charset=gb2312"); //echo $dom -> saveXML(); //保存XML文件,返回值为int(文件大小,以字节为单位) //$dom -> save("newfile.xml"); echo "<hr/>取得所有的title元素:<hr/>"; $titles = $dom -> getElementsByTagName("title"); foreach ($titles as $node){ echo $node -> textContent . "<br/>"; //这样也可以 //echo $node->firstChild->data . "<br/>"; } /* echo "<hr/>从根结点遍历所有结点:<br/>"; foreach ($dom->documentElement->childNodes as $items) { //如果节点是一个元素(nodeType == 1)并且名字是item就继续循环 if ($items->nodeType == 1 && $items->nodeName == "item") { foreach ($items->childNodes as $titles) { //如果节点是一个元素,并且名字是title就打印它. if ($titles->nodeType == 1 && $titles->nodeName == "title") { print $titles->textContent . "\n"; } } } } */ //使用XPath查询数据 echo "<hr/>使用XPath查询的title节点结果:<hr/>"; $xpath = new domxpath($dom); $titles = $xpath->query("/rss/channel/item/title"); foreach ($titles as $node){ echo $node->textContent."<br/>"; } /* 这样和使用getElementsByTagName()方法差不多,但是Xpath要强大的多 深入一点可能是这样: /rss/channel/item[position() = 1]/title 返回第一个item元素的所有 /rss/channel/item/title[@id = '23'] 返回所有含有id属性并且值为23的title /rss/channel/&folder&/title 返回所有articles元素下面的title(译者注:&folder&代表目录深度) */ //向DOM中写入新数据 $item = $dom->createElement("item"); $title = $dom->createElement("title"); $titleText = $dom->createTextNode("title text"); $title->appendChild($titleText); $item->appendChild($title); $dom->documentElement->getElementsByTagName('channel')->item(0)->appendChild($item); //从DOM中删除节点 //$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0)); //或者使用xpath查询出节点再删除 //$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0)); //$dom->save("newfile.xml"); //从DOM中修改节点数据 //修改第一个title的文件 //这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我 $firstTitle = $xpath->query("/rss/channel/item/title")->item(0); $newTitle = $dom->createElement("title"); $newTitle->appendChild(new DOMText("This's the new title text!!!")); $firstTitle->parentNode->replaceChild($newTitle, $firstTitle); //修改属性 //$firstTitle = $xpath->query("/rss/channel/item/title")->item(0); //$firstTitle->setAttribute("orderby", "4"); $dom->save("newfile.xml"); echo "<hr/><a href=\"newfile.xml\">查看newfile.xml</a>"; //下面的代码获得并解析php.net的首页,将返第一个title元素的内容。 /* $dom->loadHTMLFile("http://www.php.net/"); $title = $dom->getElementsByTagName("title"); print $title->item(0)->textContent; */ ?>
The following is the test.xml file code:
<?xml version="1.0" encoding="gb2312"?> <rss version="2.0"> <channel> <title>javascript</title> <link>http://blog.csdn.net/zhongmao/category/29515.aspx</link> <description>javascript</description> <language>zh-chs</language> <generator>.text version 0.958.2004.2001</generator> <item> <creator>zhongmao</creator> <title orderby="1">out put excel used javascript</title> <link>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</link> <pubdate>wed, 15 sep 2004 13:32:00 gmt</pubdate> <guid>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</guid> <comment>http://blog.csdn.net/zhongmao/comments/105385.aspx</comment> <comments>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback</comments> <comments>2</comments> <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspx</commentrss> <ping>http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx</ping> <description>test description</description> </item> <item> <creator>zhongmao</creator> <title orderby="2">out put word used javascript</title> <link>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</link> <pubdate>fri, 06 aug 2004 16:33:00 gmt</pubdate> <guid>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</guid> <comment>http://blog.csdn.net/zhongmao/comments/67161.aspx</comment> <comments>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback</comments> <comments>0</comments> <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspx</commentrss> <ping>http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx</ping> <description>test word description</description> </item> <item> <creator>zhongmao</creator> <title orderby="3">xmlhttp</title> <link>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</link> <pubdate>mon, 02 aug 2004 10:11:00 gmt</pubdate> <guid>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</guid> <comment>http://blog.csdn.net/zhongmao/comments/58417.aspx</comment> <comments>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback</comments> <comments>0</comments> <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/58417.aspx</commentrss> <ping>http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx</ping> <description>xmlhttpaaa asd bb cc dd</description> </item> </channel> </rss>
The above is the content of using DOM to control XML implementation code in PHP5_PHP tutorial. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

一、XML外部实体注入XML外部实体注入漏洞也就是我们常说的XXE漏洞。XML作为一种使用较为广泛的数据传输格式,很多应用程序都包含有处理xml数据的代码,默认情况下,许多过时的或配置不当的XML处理器都会对外部实体进行引用。如果攻击者可以上传XML文档或者在XML文档中添加恶意内容,通过易受攻击的代码、依赖项或集成,就能够攻击包含缺陷的XML处理器。XXE漏洞的出现和开发语言无关,只要是应用程序中对xml数据做了解析,而这些数据又受用户控制,那么应用程序都可能受到XXE攻击。本篇文章以java

当我们处理数据时经常会遇到将XML格式转换为JSON格式的需求。PHP有许多内置函数可以帮助我们执行这个操作。在本文中,我们将讨论将XML格式转换为JSON格式的不同方法。

Pythonxmltodict对xml的操作xmltodict是另一个简易的库,它致力于将XML变得像JSON.下面是一个简单的示例XML文件:elementsmoreelementselementaswell这是第三方包,在处理前先用pip来安装pipinstallxmltodict可以像下面这样访问里面的元素,属性及值:importxmltodictwithopen("test.xml")asfd:#将XML文件装载到dict里面doc=xmltodict.parse(f

1.原生js获取DOM节点:document.querySelector(选择器)document.getElementById(id选择器)document.getElementsByClassName(class选择器)....2.vue2中获取当前组件的实例对象:因为每个vue的组件实例上,都包含一个$refs对象,里面存储着对应的DOM元素或组件的引用。所以在默认情况下,组件的$refs指向一个空对象。可以先在组件上加上ref="名字",然后通过this.$refs.

xml中node和element的区别是:Element是元素,是一个小范围的定义,是数据的组成部分之一,必须是包含完整信息的结点才是元素;而Node是节点,是相对于TREE数据结构而言的,一个结点不一定是一个元素,一个元素一定是一个结点。

php5改80端口的方法:1、编辑Apache服务器的配置文件中的端口号;2、辑PHP的配置文件以确保PHP在新端口上工作;3、重启Apache服务器,PHP应用程序将开始在新的端口上运行。

在网页开发中,DOM(DocumentObjectModel)是一个非常重要的概念。它可以让开发者轻松地对一个网页的HTML或XML文档进行修改和操作,比如添加、删除、修改元素等。而PHP中内置的DOM操作库也为开发者提供了丰富的功能,本文将介绍PHP中的DOM操作指南,希望可以帮助到大家。DOM的基本概念DOM是一个跨平台、独立于语言的API,它可以将


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool