php代码中有一句$XMLdoc = newphp domdocument 安装('1.0');其中的1.0 是什么意思

PHP xml的问题_百度知道温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
三个文件:
1.root.xml
2.remove.php ——
3.replace.php ——
&child1&child1&content&/child1&&
&child2&child2&content&/child2&&
&child3&child3&content&/child3&&
&child1&child1 content&/child1&
&child2&child2 content&/child2&
&child3&child3 content&/child3&
remove.php
$xml_str&=&file_get_contents("root.xml");&//将root.xml文件中的内容读入一个字符串
$doc&=&DOMDocument::loadXML($xml_str);&&//载入这个字符串
//取得元素$child2
$root&=&$doc-&documentE
$child2&=&$root-&getElementsByTagName_r("child2")-&item(0);
//删除$child2对象
$root-&removeChild($child2);
//保存文件
$doc-&formatOutput&=&
$doc-&saveXML();
$doc-&save("root.xml");
$xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串
$doc = DOMDocument::loadXML($xml_str);
//载入这个字符串
//取得元素$child2
$root = $doc-&documentE
$child2 = $root-&getElementsByTagName_r("child2")-&item(0);
//删除$child2对象
$root-&removeChild($child2);
//保存文件
$doc-&formatOutput =
$doc-&saveXML();
$doc-&save("root.xml");
replace.php
$xml_str&=&file_get_contents("root.xml");&//将root.xml文件中的内容读入一个字符串
$doc&=&DOMDocument::loadXML($xml_str);&&//载入这个字符串
//取得元素$child3
$root&=&$doc-&documentE
$child3&=&$root-&getElementsByTagName_r("child3")-&item(0);&
//创建一个newchild元素来替代child3
$root-&replaceChild(new&DOMElement("newchild",&"new&content"),&$child3);&
//保存文件
$doc-&formatOutput&=&
$doc-&saveXML();
$doc-&save("root.xml");
$xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串
$doc = DOMDocument::loadXML($xml_str);
//载入这个字符串
//取得元素$child3
$root = $doc-&documentE
$child3 = $root-&getElementsByTagName_r("child3")-&item(0);
//创建一个newchild元素来替代child3
$root-&replaceChild(new DOMElement("newchild", "new content"), $child3);
//保存文件
$doc-&formatOutput =
$doc-&saveXML();
$doc-&save("root.xml");
使用使用XPath删除\修改节点
2.re.php ——
替换\删除节点
3.replace.xml ——
替换节点后的XML文件
4.remove.xml ——
删除节点后的XML文件
&?xml&version="1.0"?&
&&&author&Jack&Herrington&/author&
&&&title&PHP&Hacks&/title&
&&&publisher&O'Reilly&/publisher&
&&&author&Jack&Herrington&/author&
&&&title&Podcasting&Hacks&/title&
&&&publisher&O'Reilly&/publisher&
&?xml version="1.0"?&
&author&Jack Herrington&/author&
&title&PHP Hacks&/title&
&publisher&O'Reilly&/publisher&
&author&Jack Herrington&/author&
&title&Podcasting Hacks&/title&
&publisher&O'Reilly&/publisher&
$dom&=&new&DomDocument();
$dom&-&load("xml.xml");
$xpath&=&new&domxpath($dom);
//修改节点
$oldtitle&=&$xpath-&query("/books/book/title")-&item(0);
$newtitle&=&$dom-&createElement_x("title");
$newtitle-&appendChild(new&DOMText("NEW"));
$oldtitle-&parentNode-&replaceChild($newtitle,&$oldtitle);
$dom-&save("replace.xml");
//删除节点
$oldtitle&=&$xpath-&query("/books/book/title")-&item(1);
$oldtitle-&parentNode-&removeChild($oldtitle);
$dom-&save("remove.xml");
$dom = new DomDocument();
$dom -&load("xml.xml");
$xpath = new domxpath($dom);
//修改节点
$oldtitle = $xpath-&query("/books/book/title")-&item(0);
$newtitle = $dom-&createElement_x("title");
$newtitle-&appendChild(new DOMText("NEW"));
$oldtitle-&parentNode-&replaceChild($newtitle, $oldtitle);
$dom-&save("replace.xml");
//删除节点
$oldtitle = $xpath-&query("/books/book/title")-&item(1);
$oldtitle-&parentNode-&removeChild($oldtitle);
$dom-&save("remove.xml");
replace.xml&
&?xml&version="1.0"?&
&&&author&Jack&Herrington&/author&
&&&title&NEW&/title&
&&&publisher&O'Reilly&/publisher&
&&&author&Jack&Herrington&/author&
&&&title&Podcasting&Hacks&/title&
&&&publisher&O'Reilly&/publisher&
&?xml version="1.0"?&
&author&Jack Herrington&/author&
&title&NEW&/title&
&publisher&O'Reilly&/publisher&
&author&Jack Herrington&/author&
&title&Podcasting Hacks&/title&
&publisher&O'Reilly&/publisher&
remove.php&
&?xml&version="1.0"?&
&&&author&Jack&Herrington&/author&
&&&title&NEW&/title&
&&&publisher&O'Reilly&/publisher&
&&&author&Jack&Herrington&/author&
&&&publisher&O'Reilly&/publisher&
阅读(2914)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_081066',
blogTitle:'php对xml文件中节点的删除\\编辑',
blogAbstract:'\n\t\t\tphp对xml文件中节点的删除\\编辑\n\n使用getElementsByTagName_r()方法删除\\编辑XML节点\n三个文件:\n\n1.root.xml\n2.remove.php ——\n删除节点\n3.replace.php ——\n替换节点\n\n\nroot.xml\n\n&root&&\n&child1&child1&content&/child1&&\n',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:8,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}投诉建议、广告投放、友情链接,联系QQ:
php中DOMDocument与SimpleXML创建与解析xml程序
Loading...
100 次阅读
通过DOM扩展创建一个DOMDocument对象。通过DOMDocument对象的方法向文档添加元素,最后通过save()或saveXML()方法来生成文档,也可以通过SimpleXML来快速解析xml文档。 例子:
DOM XML 解析器函数是 PHP 核心的组成部分。无需安装就可以使用这些函数。
将在我们的例子中使用下面的 XML 文件:
&?xml version=&1.0& encoding=&ISO-8859-1&?&
&to&George&/to&
&from&John&/from&
&heading&Reminder&/heading&
&body&Don't forget the meeting!&/body&
加载和输出 XML
我们需要初始化 XML 解析器,加载 XML,并把它输出:
$xmlDoc = new DOMDocument();
$xmlDoc-&load(&note.xml&);
print $xmlDoc-&saveXML();
以上代码的输出:
George John Reminder Don't forget the meeting!假如您在浏览器窗口中查看源代码,会看到下面这些 HTML:
&?xml version=&1.0& encoding=&ISO-8859-1&?&
&to&George&/to&
&from&John&/from&
&heading&Reminder&/heading&
&body&Don't forget the meeting!&/body&
面的例子创建了一个 DOMDocument-Object,并把 &note.xml& 中的 XML 载入这个文档对象中。
saveXML() 函数把内部 XML 文档放入一个字符串,这样我们就可以输出它。
//创建一个新文档
$dom = new DOMDocument('1.0');
//创建根元素book,并将其添加到文档中
$book = $dom-&appendChild($dom-&createElement('book'));
//创建一个title元素,并将其添加到$book中
$title = $book-&appendChild($dom-&createElement('title'));
$title-&appendChild($dom-&createTextNode('Php Book'));
//为title元素的添加属性cover,属性值soft
$title-&setAttribute('cover','soft');
//创建author元素,并将其添加到$book中
$kaifabu = $book-&appendChild($dom-&createElement('author'));
//为author元素添加文本
$kaifabu-&appendChild($dom-&createTextNode('kaifabu'));
//创建author元素,并将其添加到$book中
$kaifabu = $book-&appendChild($dom-&createElement('author'));
//为author元素添加文本
$kaifabu-&appendChild($dom-&createTextNode('Younker'));
//将格式化的DOM文档输出为XML
$dom-&formatOutput =
//生成books.xml文件
$dom-&save('books.xml');
//此步把生成的XML以字符串形式显示
echo htmlentities($dom-&saveXML());
输出结果如下:
&?xml version=&1.0& ?&
& &title cover=&soft&&Php Book&/title&
& &author&kaifabu&/author&
& &author&Younker&/author&
注:如果上面生成XML这步改成$dom-&save(&books.xml&);直接生成一个books.xml文件
下面我们利用simplexml来解析xml文件
SimpleXML 函数允许您把 XML 转换为对象。
通过普通的属性选择器或数组迭代器,可以处理这个对象,就像处理任何其他对象一样。
其中的一些函数需要最新的 PHP 版本。
SimpleXML 函数是 PHP 核心的组成部分。无需安装即可使用这些函数。
//使用SimpleXML扩展解析XML文档,解析出来是一个对象,可以通过对象获取它的元素内容
$sx = simplexml_load_file('books.xml');
//获取title标签的内容
$title = $sx-&
//因为有两个author元素,通过循环获取每个author元素内容
$authors = '';
foreach($sx-&author as $author)
&$authors .= '&' . $
$authors = substr($authors, 1);
echo $title . ':' . $
输出结果如下:
Php Book:kaifabu&Younker
PHP:指示支持该函数的最早的 PHP 版本。
SimpleXML函数
__construct() 创建一个新的 SimpleXMLElement 对象。 5
addAttribute() 给 SimpleXML 元素添加一个属性。 5
addChild() 给 SimpleXML 元素添加一个子元素。 5
asXML() 从 SimpleXML 元素获取 XML 字符串。 5
attributes() 获取 SimpleXML 元素的属性。 5
children() 获取指定节点的子。 5
getDocNamespaces() 获取 XML 文档的命名空间。 5
getName() 获取 SimpleXML 元素的名称。 5
getNamespaces() 从 XML 数据获取命名空间。 5
registerXPathNamespace() 为下一次 XPath 查询创建命名空间语境。 5
simplexml_import_dom() 从 DOM 节点获取 SimpleXMLElement 对象。 5
simplexml_load_file() 从 XML 文档获取 SimpleXMLElement 对象。 5
simplexml_load_string() 从 XML 字符串获取 SimpleXMLElement 对象。 5
xpath() 对 XML 数据运行 XPath 查询。 5
IT技术书籍推荐:
算法导论(原书第3版)
Thomas H.Cormen (作者), Charles E.Leiserson (作者), Ronald L.Rivest (作者), Clifford Stein (作者), 殷建平 (译者), 徐云 (译者), 王刚 (译者), 等 (译者)
《算法导论(原书第3版)》内容简介:在有关算法的书中,有一些叙述非常严谨,但不够全面;另一些涉及了大量的题材,但又缺乏严谨性。《算法导论(原书第3版)》将严谨性和全面性融为一体,深入讨论各类算法,并着力使这些算法的设计和分析能为各个层次的读者接受。全书各章自成体系,可以作为独立的学习单元;算法以英语和伪代码的形式描述,具备初步程序设计经验的人就能看懂;说明和解释力求浅显易懂,不失深度和数学严谨性。全书选材经典、内容丰富、结构合理、逻辑清晰,对本科生的数据结构课程和研究生的算法课程都是非常实用的教材,在IT专业人员的职业生涯中,《算法导论(原书第3版)》也是一本案头必备的参考书或工程实践手册。
相关推荐:1、2、3、4、5、6、7、8、9、10、
随机关键词:
正在加载...
最新微信头条PHP操作XML时的问题_百度知道

我要回帖

更多关于 dom039 藤井らん 的文章

 

随机推荐