Home > Backend Development > PHP Tutorial > Using DOM to control XML in PHP5(2)

Using DOM to control XML in PHP5(2)

黄舟
Release: 2023-03-03 14:10:02
Original
890 people have browsed it

//Write new data to 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);

//Remove node from DOM
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName(" channel")->item(0));
//Or use xpath to query the node and delete it
//$dom->documentElement->RemoveChild($xpath->query("/rss/channel" )->item(0));
//$dom->save("newfile.xml");

//Modify node data from DOM
//Modify the first title file
// This place is clumsy. Create a new node and then replace the old node. If anyone has other good methods, please tell me
$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);
//Modify attributes
//$firstTitle = $xpath->query("/rss/channel/item/title")->item(0);
//$firstTitle->setAttribute( "orderby", "4");
$dom->save("newfile.xml");

echo "


View newfile.xml< ;/a>";

//The following code obtains and parses the homepage of php.net and returns the content of the first title element.
/*
$dom->loadHTMLFile("http://www.php.net/");
$title = $dom->getElementsByTagName("title");
print $title->item( 0)->textContent;
*/
?>

The following is the test.xml file code:




javascript
http://blog.csdn.net/zhongmao/category/29515.aspx
javascript
zh-chs
.text version 0.958.2004.2001

zhongmao
< ;title orderby="1">out put excel used javascript
http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx
wed, 15 sep 2004 13:32:00 gmt
http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx
http://blog.csdn.net/zhongmao/comments/105385.aspx
http://blog.csdn.net/zhongmao/archive/2004/09 /15/105385.aspx#feedback
2
http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspx
http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx
test description


zhongmao
out put word used javascript
http://blog.csdn.net/zhongmao/archive/2004 /08/06/67161.aspx
fri, 06 aug 2004 16:33:00 gmt
  http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx 
  http://blog.csdn.net/zhongmao/comments/67161.aspx 
  http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback 
  0 
  http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspx 
  http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx 
  test word description 
 
 
   
  zhongmao 
  xmlhttp 
  http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx 
  mon, 02 aug 2004 10:11:00 gmt 
  http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx 
  http://blog.csdn.net/zhongmao/comments/58417.aspx 
  http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback 
  0 
  http://blog.csdn.net/zhongmao/comments/commentrss/58417.aspx 
  http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx 
  xmlhttpaaa asd bb cc dd 
 
 
 
 
 

以上就是在PHP5中使用DOM控制XML(2)的内容,更多相关文章请关注PHP中文网(m.sbmmt.com)!


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template