<timezone>
<id>hell</id>
<raw_offset>2</raw_offset>
<dst>
<start_month>02</start_month>
<start_day>29</start_day>
<start_day_of_week>00</start_day_of_week>
<start_time_hour>02</start_time_hour>
<start_time_minute>00</start_time_minute>
<end_month>09</end_month>
<end_day>06</end_day>
<end_day_of_week>00</end_day_of_week>
<end_time_hour>02</end_time_hour>
<end_time_minute>00</end_time_minute>
</dst>
</timezone>我要取到start_month,start_day...等等这些数值怎么取。。谢谢各位达人帮忙解答。。

解决方案 »

  1.   

    $s=<<<xml
    <timezone>
    <id>hell</id>
    <raw_offset>2</raw_offset>
    <dst>
    <start_month>02</start_month>
    <start_day>29</start_day>
    <start_day_of_week>00</start_day_of_week>
    <start_time_hour>02</start_time_hour>
    <start_time_minute>00</start_time_minute>
    <end_month>09</end_month>
    <end_day>06</end_day>
    <end_day_of_week>00</end_day_of_week>
    <end_time_hour>02</end_time_hour>
    <end_time_minute>00</end_time_minute>
    </dst>
    </timezone>
    xml;
    preg_match_all('/<(start_month|start_day)>(.+?)<\/(start_month|start_day)>/s',$s,$m);
    print_r($m);
      

  2.   


    //如果xml是文件的话
    if (file_exists('test.xml')) {
        $xml = simplexml_load_file('test.xml');
     
        print_r($xml);
    } else {
        exit('Failed to open test.xml.');
    }
    //如果是文本的话
    $string = <<<XML
    <?xml version='1.0'?> 
    <timezone>
    <id>hell</id>
    <raw_offset>2</raw_offset>
    <dst>
    <start_month>02</start_month>
    <start_day>29</start_day>
    <start_day_of_week>00</start_day_of_week>
    <start_time_hour>02</start_time_hour>
    <start_time_minute>00</start_time_minute>
    <end_month>09</end_month>
    <end_day>06</end_day>
    <end_day_of_week>00</end_day_of_week>
    <end_time_hour>02</end_time_hour>
    <end_time_minute>00</end_time_minute>
    </dst>
    </timezone>
    XML;
    $xml = simplexml_load_string($string);
    print_r($xml);
      

  3.   

    谢谢大家的关注,我想通过dom解析。$dst_list = $timeZoneList->item($j)->getElementsByTagName('dst');
    foreach ($dst_list->childNodes AS $item)
    {
    $startMonth = $item->nodeValue;
    }
    但是就是读不出来。。不知道为什么。。
      

  4.   

    private function writeOrgin(){
    //将数组输出到XML文件中
    // by MoreWindows( http://blog.csdn.net/MoreWindows )
    $article_array = array(
    "第一篇" => array(
    "title"=>"PHP访问MySql数据库 初级篇",
    "link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
    ),
    "第二篇" => array(
    "title"=>"PHP访问MySql数据库 中级篇 Smarty技术",
    "link"=>"http://blog.csdn.net/morewindows/article/details/7094642"
    ),
    "第三篇" => array(
    "title"=>"PHP访问MySql数据库 高级篇 AJAX技术",
    "link"=>"http://blog.csdn.net/morewindows/article/details/7086524"
    ),
    );
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->formatOutput = true;
    $rootelement = $dom->createElement("MoreWindows");
    foreach ($article_array as $key=>$value)
    {
    $article = $dom->createElement("article", $key);
    $title = $dom->createElement("title", $value['title']);
    $link = $dom->createElement("link", $value['link']);
    $article->appendChild($title);
    $article->appendChild($link);
    $rootelement->appendChild($article);
    }
    $dom->appendChild($rootelement);
    $filename = "D:\\test.xml";
    echo 'XML文件大小' . $dom->save($filename) . '字节';

    }*/
    // 不使用
    /*
    private function readOrgin(){
    $filename = "D:\\test.xml";
    $article_array = array();

    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->load($filename);

    //得到<article>结点
    $articles = $dom->getElementsByTagName("article");
    echo '<article> 结点个数 ' . $articles->length;
    foreach ($articles as $article)
    {
    $id = $article->getElementsByTagName("id")->item(0)->nodeValue;
    $title = $article->getElementsByTagName("title")->item(0)->nodeValue;
    $link = $article->getElementsByTagName("link")->item(0)->nodeValue;
    $article_array[$id] = array('title'=>$title, 'link'=>$link);
    }

    //输出结果
    echo "<pre>";
    var_dump($article_array);
    echo "</pre>";
    }参考下这个,当初从别的地方抄来,自己改改应该就能用了
      

  5.   

    $doc_upp = new DOMDocument();
    $doc_upp->load( 'xml.xml' );
    $item_note = $doc_upp->getElementsByTagName("dst");
    foreach($item_note as $val){
    $start_month = $val->getElementsByTagName("start_month")->item(0)->nodeValue;
    $start_day = $val->getElementsByTagName("start_day")->item(0)->nodeValue;
    ……
    }