您的位置:首页 > 其它

解析xml

2015-11-21 09:55 302 查看
<?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');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: