php 操作xml

<?php

$xml = simplexml_load_file("article.xml");
//var_dump($xml);
foreach ($xml->children() as $item) { //循环读取 depart标签下的子标签
    var_dump($item->attributes()->id);
//   foreach($item->attributes() as $attr) {
////       var_dump($attr);
//       echo $attr->nodeName. ' : '. $attr->nodeValue;
//    echo "<br/>";
//   }
   
   
//   foreach ($rootdata->attributes as $attrib)
//{
//$attribName = $attrib->nodeName;   //nodeName为属性名称
//$attribValue = $attrib->nodeValue; //nodeValue为属性内容
//}
    
    echo $item->title;
    echo "<br/>";
    
} 
exit();

/* -------------  make -------------- */
$dom = new DOMDocument("1.0", "utf-8");
header("Content-Type: text/plain");
$root = $dom->createElement("article");
$dom->appendChild($root);

for($i=1; $i<3; $i++) {
    $item = $dom->createElement("item");
    $root->appendChild($item);
    $title = $dom->createElement("title");
    $item->appendChild($title);
    $text = $dom->createTextNode("This is the article's title ");
    $title->appendChild($text);
    // create attribute
    $id = $dom->createAttribute("id");
    $item->appendChild($id);
    $idValue = $dom->createTextNode($i);
    $id->appendChild($idValue);
    // create CDATA section
    $content = $dom->createElement("content");
    $item->appendChild($content);
    $contentData = <<<EOF
    <div class="newsItem">
            <div id="blog-news"><div style="margin-top:90px"><a title="站长统计" target="_blank" href="http://www.cnzz.com/stat/website.php?web_id=3818781"><img vspace="0" hspace="0" border="0" src="http://icon.cnzz.com/img/pic.gif"></a>
    </div>


    <div style="POSITION: absolute; TOP:8px;margin-left:300px;text-align:right; display:none" id="gplus">
    <div id="__document_write_ajax_div-2" style="display: none"></div>
    <g:plusone></g:plusone>
    </div>

    <div style="POSITION: absolute; TOP:180px;margin-left:-45px;opacity: 0.7;width:336px;height:280px;font-size:12px;filter:alpha(opacity=30);" id="myAd">

    <div id="__document_write_ajax_div-3" style="display: none"></div>
    <div id="__document_write_ajax_div-4" style="display: none"></div>

    </div>

    <div id="__document_write_ajax_div-5" style="display: none"></div><div id="profile_block">昵称:<a href="http://home.cnblogs.com/u/mfryf/">知识天地</a><br>园龄:<a title="入园时间:2012-02-11" href="http://home.cnblogs.com/u/mfryf/">1年10个月</a><br>粉丝:<a href="http://home.cnblogs.com/u/mfryf/followers/">28</a><br>关注:<a href="http://home.cnblogs.com/u/mfryf/followees/">1</a><div id="p_b_follow"></div><div id="__document_write_ajax_div-6" style="display: none"></div></div></div><script type="text/javascript">loadBlogNews();</script>
    </div>        
EOF;
    $cdata = $dom->createCDATASection($contentData);
    $content->appendChild($cdata);
}

// save and display tree
//$dom->save("article.xml");
echo $dom->saveXML();

/* -------------  make end -------------- */
?> 

 

posted on 2013-12-23 11:44  bandbandme  阅读(252)  评论(0编辑  收藏  举报