xml mysql语法_php 操作xml基本语法

header('content-type:text/html;charset=utf-8');

// DOM 技术解析xml文件,实际就是读取显示xml文件的内容

$xml = new DOMDocument();

$xml->load('book.xml');

$books = $xml->getElementsByTagName("book");

foreach($books as $b){

echo $b->getAttribute('id');

echo $b->getElementsByTagName('name')->item(0)->nodeValue;

echo $b->getElementsByTagName('price')->item(0)->nodeValue;

echo '


';

}

//建立xml文件基本方法

$xmlinfo= <<

PHP技术开发

80

Oracle技术

120

MySQL数据库技术

89

Java项目实例

90

Ajax技术开发

50

aaa;

//file_put_contents('b.xml',$xmlinfo);

//利dom方式建立xml文件

/*

$x = new DOMDocument(1.0,"utf-8");

$x->formatOutput=true;

//建立根元素

$root = $x->createElement('books');

//建立元素

$b1 = $x->createElement('book');

$id1 = $x->createAttribute('id');

$id1v = $x->createTextNode('100');

$bn = $x->createElement('name','php开发');

$bp = $x->createElement('price',80);

$id1->appendChild($id1v);

$b1->appendChild($id1);

$b1->appendChild($bn);

$b1->appendChild($bp);

$root->appendChild($b1);

$x->appendChild($root);

$x->save('s.xml');

*/

//删除s.xml文件中的一个元素

$doc = new DOMDocument(1.0,'utf-8');

$doc->formatOutput = true;

$doc->load('s.xml');

//$root= $doc->getElementsByTagName("books")->item(0);

//$book = $doc->getElementsByTagName('book')->item(2);

//$root->removeChild($book);

//$book = $doc->getElementsByTagName('book')->item(0);

//$book->parentNode->removeChild($book);

//添加元素

$idv = $doc->createTextNode($doc->getElementsByTagName('book')->length+100);

$id = $doc->createAttribute('id');

$id->appendChild($idv);

$book = $doc->createElement('book');

$name = $doc->createElement('name','java项目');

$price = $doc->createElement('price',100);

$book->appendChild($id);

$book->appendChild($name);

$book->appendChild($price);

$root = $doc->getElementsByTagName("books")->item(0);

$root->appendChild($book);

$doc->save('s.xml');

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值