當前位置:首頁 » 編程語言 » 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-10 08:43:23 瀏覽:994
小型車有哪些配置 發布:2024-05-10 08:38:56 瀏覽:525
安卓暢享8a怎麼拿出手機卡 發布:2024-05-10 08:18:25 瀏覽:185
怎麼搭建區域網資料庫伺服器配置 發布:2024-05-10 08:17:29 瀏覽:657
安卓系統手機怎麼解鎖 發布:2024-05-10 08:17:27 瀏覽:472
php數組循環輸出 發布:2024-05-10 08:17:18 瀏覽:677
安卓手機助手導通訊錄哪個好 發布:2024-05-10 08:15:49 瀏覽:281
安卓微信在哪裡設置 發布:2024-05-10 08:14:22 瀏覽:213
蘋果抖音如何找回安卓上傳的視頻 發布:2024-05-10 08:13:42 瀏覽:438
php學法網 發布:2024-05-10 07:56:10 瀏覽:210