当前位置:首页 » 编程语言 » phpxml字符串

phpxml字符串

发布时间: 2022-05-06 18:19:32

1. php 如何解析xml格式字符串

//假设xml字符串
$postStr = '<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[FromUser]]></FromUserName><CreateTime>123456789</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[p1]]></Content></xml>';
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
var_mp($postObj);

/**
object(SimpleXMLElement)[16] public 'ToUserName' => string 'toUser' (length=6) public 'FromUserName' => string 'FromUser' (length=8) public 'CreateTime' => string '123456789' (length=9) public 'MsgType' => string 'text' (length=4) public 'Content' => string 'p1' (length=2)
*/
//把xml字符串解析为对象

2. 使用php循环出xml中多个相同重复的标签

重复的 xml 元素节点可以用 foreach 循环取出重复元素。

示例:

// $xmlData = file_get_contents('items.xml');

$xmlElement=simplexml_load_string($xmlData, 'SimpleXMLElement', 'LIBXML_NOCDATA');

$elements = $xmlElement->CustAcctId;

foreach ($elements as $element) {

var_mp($element);

}

提示:使用simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA) 解析 xml 字符串,如果是 xml 文件,可以 file_get_contents 读取文件。

3. PHP 怎么把一个XML节点字符串直接插入到XML中啊 求教各位大神

我是用DOMDocument对象来操作xml的 代码如下

<?php
//加载xml
$path=$_SERVER["DOCUMENT_ROOT"].'/20150524/book.xml';

//实例化类
$books=newDOMDocument();
//通过方法加载
$books->load($path);
//添加元素/属性
$newItem=$books->createElement('item');//创建新元素

$title=$books->createElement('title');//创建子元素
$title->nodeValue='newtitle';
$newItem->appendChild($title);//把子元素添加到父元素上

$content=$books->createElement('content');//创建子元素
$content->nodeValue='newcontent';
$newItem->appendChild($content);//把子元素添加到父元素上

//添加到第一个节点前
$books->documentElement->insertbefore($newItem,$elements->item(0));
$books->save($path);//保存
?>

添加后 xml文件如下所示

<?xmlversion="1.0"?>
<books>
<item>
<title>newTitle</title>
<content>newContent</content>
</item>
<bookname="JavaScript:TheDefiitiveGuide"publisher="O'ReillyMedia,Inc.">
<author>DavidFlanagan</author>
</book>
<bookname="PHPanfMySQLWebDevelopment"publisher="PerasonEcation">
<author>LukeWelling</author>
<author>LauraThomson</author>
</book>
<bookname="HTTP:TheDefiitiveGuide"publisher="O'ReillyMedia,Inc.">
<author>DavidCourley</author>
<author>BrianTotty</author>
</book>
</books>

4. php将XML转换成字符串!

$str = $xml->asXML();

$str就是你要的字符串

5. PHP 读取xml格式的字符串

$strXml='<?xml version="1.0" encoding="utf-8" ?><responses code="200" msg="time: 0ms 971us"><response>法国</response><response>德国</response><response>中国</response><response>英国</response><response>泰国</response><response>美国</response><response>韩国</response><response>西兰公国</response><response>中非共和国</response><response>韩国丽水-韩国</response></responses>';

$pos = strpos($strXml, 'xml');
if (!$pos) {
die("不是xml字符串!");
}
$obj=simplexml_load_string($strXml,'SimpleXMLElement', LIBXML_NOCDATA);
if(is_object($obj)){
$obj=get_object_vars($obj);
}

echo "<pre>";
print_r($obj);

另外记得文件一定要是utf-8编码,如果不是记得用iconv函数转一下。

6. php中将xml文件读入一个字符串

试试看:
$content = file_get_contents($file);
echo str_replace('<','&lt;',$content);

7. php echo xml字符串到屏幕,为啥只显示标签内容而不显示标签

输出到页面的内容是
<?xml
version=\"1.0\"
encoding=\"GBK\"
?><API><in><orderNum>20391201319</orderNum><Date>20130924</Date></in></API>
浏览器能识别这xml格式,当然只会显示内容啊!
如果有接口调用这个
传递过去的是XML的完整形式,不需要担心!

8. 如何在php文件里写xml

php文件里写xml方法:

1、Xml代码


<?php
$data_array=array(
array(
'title'=>'title1',
'content'=>'content1',
'pubdate'=>'2009-10-11',
),
array(
'title'=>'title2',
'content'=>'content2',
'pubdate'=>'2009-11-11',
)
);
//属性数组
$attribute_array=array(
'title'=>array(
'size'=>1
)
);
$string=<<<XML
<?xmlversion='1.0'encoding='utf-8'?>
<article>
</article>
XML;
$xml=simplexml_load_string($string);
foreach($data_arrayas$data){
$item=$xml->addChild('item');
if(is_array($data)){
foreach($dataas$key=>$row){
$node=$item->addChild($key,$row);
if(isset($attribute_array[$key])&&is_array($attribute_array[$key]))
{
foreach($attribute_array[$key]as$akey=>$aval){
//设置属性值
$node->addAttribute($akey,$aval);
}
}
}
}
}
echo$xml->asXML();
?>

9. php把xml转换为字符串

楼主我教你吧,首先xml文件里的内容为

<?xmlversion="1.0"encoding="ISO-8859-1"?>
<content
<name>lishi</name>
<age>17</age>
</content>

读取xml文件内容

$str=file_get_contents($xml);$xml为xml文件路径地址


将读取的字符串内容转化为xml对象

$obj=simplexml_load_string($str)


操作对象里的数据

$obj->name="lishi111";

$obj->age=77;


拼接新的字符串

$strNew="<?xml version='1.0' encoding='ISO-8859-1'?>";

$str.="<content><name>".$obj->name."</name";

$str.="<age>".$obj->age."</age></content>";


将新的字符串写入xml文件

file_put_content($xml,$strNew);


最后一步拿分来吧,哈哈哈哈。有问题继续。。。

10. 如何用php给XML字符串的子节点添加属性

  • xml文件:

    <?xml version="1.0" encoding="UTF-8" ?>

    <clientSet>

    <server url="192.168.0.180" port="1935" />

    <rootPath value="" />

    <homePath value="ht.com" />

    <helpPath value="help.html" />

    <language value="en" />

    <theme value="default" />

    <visibleMarquee value = "true" />

    <visibleWhitePaper value="true" />

    <showMemberRoomForGuest value = "true" />

    <emotions enabled="true" column="5" autoPlay="false">

    <item name="Birthday" src="cartoon/movie/birthday.swf" thumb="cartoon/preview/birthday-small.swf" ration="15"/>

    <item name="Boom" src="cartoon/movie/boom.swf" thumb="cartoon/preview/boom-small.swf" ration="6"/>

    <item name="Bubble" src="cartoon/movie/bubble.swf" thumb="cartoon/preview/bubble-small.swf" ration="7.5"/>

    <item name="Cry" src="cartoon/movie/cry.swf" thumb="cartoon/preview/cry-small.swf" ration="5.4"/>

    <item name="Doggie" src="cartoon/movie/doggie.swf" thumb="cartoon/preview/doggie-small.swf" ration="13"/>

    <item name="Greeting" src="cartoon/movie/greeting.swf" thumb="cartoon/preview/greeting-small.swf" ration="7.4"/>

    <item name="Football" src="cartoon/movie/football.swf" thumb="cartoon/preview/football-small.swf" ration="2.2"/>

    </emotions >

    </clientSet>

  • php代码:

    <?

    $dom=new DOMDocument('1.0');

    $dom->load('x.xml');

    $em=$dom->getElementsByTagName('emotions');

    $em=$em->item(0);

    $items=$em->getElementsByTagName('item');

    foreach($items as $a){

    foreach($a->attributes as $b){

    if($b->nodeValue=='Birthday'){

    $a->setAttribute('name','nBirthday');

    }

    }

    }

    $t=$dom->createElement('item');

    $t->setAttribute('name','x');

    $t->setAttribute('src','www..com');

    $t->setAttribute('ration','ration');

    $em->appendChild($t);

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

    ?>

  • PHP解析XML文档属性并编辑:

    <?php

    //读取xml

    $dom=new DOMDocument('1.0');

    $dom->load('data.xml');

    $em=$dom->getElementsByTagName('videos');//最外层节点

    $em=$em->item(0);

    $items=$em->getElementsByTagName('video');//节点

    //如果不用读取直接添加的话把下面这一段去掉即可

    foreach($items as $a){

    foreach($a->attributes as $b){//$b->nodeValue;节点属性的值$b->nodeName;节点属性的名称

    echo $b->nodeName;

    echo ":";

    echo $b->nodeValue;

    echo "<br/>";

    }

    }

    //下面是往xml写入一行新的

    $t=$dom->createElement('video');//<video

    $t->setAttribute('title','1');//<video name="data"

    $t->setAttribute('src','2');//<video name="data" src="2"

    $t->setAttribute('img','1');//<video name="data" img="1"

    $em->appendChild($t);//<video name="data" img="1"/>

    $dom->save('data.xml');

    ?>

    当时的xml文档:

    <?xml version="1.0"?>

    <videos>

    <video img="a" url="1" title="1" nickname="1" tag="1" vid="1" star="1"/>

    <video img="b" url="2" title="2" nickname="2" tag="2" vid="2" star="2"/>

    <video img="c" url="3" title="3" nickname="3" tag="3" vid="3" star="3"/>

    <video title="d" src="2" img="1"/>

    </videos>

  • //下面这一个文件是后改的可以修改xml:

    <?php

    $doc = new DOMDocument();

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

    //查找 videos 节点

    $root = $doc->getElementsByTagName('videos');

    //第一个 videos 节点

    $root = $root->item(0);

    //查找 videos 节点下的 video 节点

    $userid = $root->getElementsByTagName('video');

    //遍历所有 video 节点

    foreach ($userid as $rootdata)

    {

    //遍历每一个 video 节点所有属性

    foreach ($rootdata->attributes as $attrib)

    {

    $attribName = $attrib->nodeName; //nodeName为属性名称

    $attribValue = $attrib->nodeValue; //nodeValue为属性内容

    //查找属性名称为ip的节点内容

    if ($attribName =='img')

    {

    //查找属性内容为ip的节点内容

    if ($attribValue =='1')

    {

    //将属性为img,img内容为1的修改为image;

    $rootdata->setAttribute('img','image');

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

    }

    }

    }

    }

    ?>

热点内容
编译静态函数时 发布:2024-05-20 21:51:20 浏览:350
如何在别人的服务器加模组 发布:2024-05-20 21:28:29 浏览:60
服务器的bios芯片电脑店有吗 发布:2024-05-20 21:28:26 浏览:223
剪辑电影什么配置 发布:2024-05-20 21:25:17 浏览:818
解压神器中的诡异事件 发布:2024-05-20 21:17:59 浏览:7
星火草原系统源码 发布:2024-05-20 21:12:44 浏览:767
c编译器手机版中文版下载 发布:2024-05-20 21:11:56 浏览:777
存储超融合 发布:2024-05-20 21:07:24 浏览:455
孩子培训编程 发布:2024-05-20 21:05:57 浏览:455
linux服务器源码 发布:2024-05-20 21:05:00 浏览:400