解析xml

<?php
    $_a = 5;


    //单引号
    $_string1 = 'This is $_a
    a String';


    //双引号
    $_string2 = "This is $_a
    a String";


    // echo $_string1;


    // echo $_string2;


//复杂式,多行, 特殊字符 , 双引号, 变量。
    //<<<aaaa表示开始 aaaa表示结束(前面不能有任意字符);
//     $_string3 = <<<aaaa


//     <321321>
//     121232132*&*^*321'3213"
//     $_a;13;
// aaaa;


    // echo $_string3;


    $_xml = <<<_xml
<?xml version="1.0" encoding="utf-8"?>
<root>
    <version>1.0</version>
    <info>xml解析测试</info>


    <user>
        <name>飘城Web俱乐部</name>
        <url>http://www.yc60.com</url>
        <author sex="男">sora</author>
    </user>


    <user>
        <name>北风</name>
        <url>http://www.veryod.com</url>
        <author sex="女">candy</author>
    </user>
</root>
_xml;


// echo $_xml;


//创建一个Simplexml对象 , 传入xml字符串


// $_sxe = simplexml_load_string($_xml);


//生成xml
// $_xml = $_sxe->asXML('test.xml');


//反射所有的函数属性
// Reflection::export(new ReflectionClass($_sxe));
//print_r($_sxe);




// $_sxe = simplexml_load_file('test.xml');


//打印一级标签
// echo $_sxe->version;


//如果有多个一级标签  $_sxe->version 是数组,但不能用print_r();


//如果多层标签
// echo $_sxe->user[1]->name[0];


//属性
// echo  $_sxe->user[1]->author->attributes();




//使用xpth来获取xml节点操作


//载入xml
// $_sxe = simplexml_load_file('test.xml');


//获取version 的值


// $_version = $_sxe->xpath('/root/version');


// print_r($_version);


// echo $_version[0];


// print_r($_sxe->xpath('/root/user'));




//DOMDocument 解析xml


//创建Dom对象
// $_doc = new DOMDocument();


//载入xml文件
// $_doc->load('test.xml');


// $_version = $_doc->getElementsByTagName('version');


// echo $_version->item(0)->nodeValue;


//使用dom创建xml


// 声明domdocument对象
$_doc  = new DOMDocument('1.0', 'utf-8');


//使用排版格式
$_doc->formatOutput = true;


$_root = $_doc->createElement('root');


$_version = $_doc->createElement('version');


$_versionTextNode = $_doc->createTextNode('1.0');


$_version->appendChild($_versionTextNode);


$_root->appendChild($_version);


$_doc->appendChild($_root);
//生成xml
$_doc->save('aaa.xml');



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值