大概我弄出来是这样子了:
<?xml version="1.0" encoding = "gb2312"?>
<category name="新闻网">
<site>
<name>导航网</name>
<url>http://www.CCCC.com/</url>
</site>
<site>
<name>新浪新闻</name>
<url>http://news.sina.com.cn/</url>
</site>
<site>
<name>凤凰卫视新闻</name>
<url>http://news.phoenixtv.com/</url>
</site>
</category>
<category name="IT网站">
<site>
<name>太平洋</name>
<url>http://www.XXXX.com/</url>
</site>
<site>
<name>泡泡</name>
<url>http://www.YYYY.com/</url>
</site>
</category>
</xml>
现在怎么用PHP把它读出来?
并且怎么用PHP把原来的代码转换成上面的xml?

解决方案 »

  1.   

    我的建议是:你最上面的代码要么存在数据库中,要么就用正则慢慢匹配全部按类别提出来!
    然后用php按照类别,例如category_name site_name site_url这3个类别生成xml!
      

  2.   

    to hnxxwyq(独自流浪) 可不可以把:
    <<新闻网>>导航网<http://www.CCCC.com/>新浪新闻<http://news.sina.com.cn/>凤凰卫视新闻<http://news.phoenixtv.com/>关注健康<http://www.39.net/><<IT网站>>太平洋<http://www.XXXX.com/>泡泡<http://www.YYYY.com/><<生活网>>导航网<http://www.CCCC.com/>新浪新闻<http://news.sina.com.cn/>凤凰卫视新闻<http://news.phoenixtv.com/>关注健康<http://www.39.net/><<体育网>>太平洋<http://www.XXXX.com/>泡泡<http://www.YYYY.com/>直接转换成上面这样的xml?
    应该如何转的?
      

  3.   

    <?php$str = "<<新闻网>>导航网<http://www.CCCC.com/>新浪新闻<http://news.sina.com.cn/>凤凰卫视新闻<http://news.phoenixtv.com/>关注健康<http://www.39.net/>
    <<IT网站>>太平洋<http://www.XXXX.com/>泡泡<http://www.YYYY.com/>
    <<生活网>>导航网<http://www.CCCC.com/>新浪新闻<http://news.sina.com.cn/>凤凰卫视新闻<http://news.phoenixtv.com/>关注健康<http://www.39.net/>
    <<体育网>>太平洋<http://www.XXXX.com/>泡泡<http://www.YYYY.com/>";
    $strArray = $strGroup = $strlink = $strUrl = array();//获取组名字
    preg_match_all('/(<<.+?\>>)/s', $str, $strArray);
    $strGroup = $strArray[1];
    print_r($strGroup);//获取链接名字和地址
    $strArray = array();
    foreach ($strGroup as $key => $group) {
    preg_match_all('/('.$group.'.+?<<)/s', $str, $strArray);
    $str = preg_replace('/('.$group.'.+?<<)/s', '<<', $str);
    $strUrl[$key] = substr($strArray[0][0], strlen($strGroup[$key]), -2);
    }$strUrl[$key] = substr($str, strlen($strGroup[$key]));
    //开始分割
    foreach($strUrl as $key => $url) {
    $strArray = array();
    $strArray = explode('>', $url);
    foreach($strArray as $tmp) {
    $strlink[$key][] = $tmp ? explode('<', $tmp) : '';
    }
    }print_r($strGroup);
    print_r($strlink);
    ?>效率一般,只是按照特征来编写的,如果不是顶楼的特征就无能无力了!
      

  4.   

    $strGroup  记录的是category nameArray
    (
        [0] => <<新闻网>>
        [1] => <<IT网站>>
        [2] => <<生活网>>
        [3] => <<体育网>>
    )$strlink 记录的是 0 name 1 url(和组正好关联)
    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [0] => 导航网
                        [1] => http://www.CCCC.com/
                    )            [1] => Array
                    (
                        [0] => 新浪新闻
                        [1] => http://news.sina.com.cn/
                    )            [2] => Array
                    (
                        [0] => 凤凰卫视新闻
                        [1] => http://news.phoenixtv.com/
                    )            [3] => Array
                    (
                        [0] => 关注健康
                        [1] => http://www.39.net/
                    )            [4] => Array
                    (
                        [0] => 

                    )        )    [1] => Array
            (
                [0] => Array
                    (
                        [0] => 太平洋
                        [1] => http://www.XXXX.com/
                    )            [1] => Array
                    (
                        [0] => 泡泡
                        [1] => http://www.YYYY.com/
                    )            [2] => Array
                    (
                        [0] => 

                    )        )    [2] => Array
            (
                [0] => Array
                    (
                        [0] => 导航网
                        [1] => http://www.CCCC.com/
                    )            [1] => Array
                    (
                        [0] => 新浪新闻
                        [1] => http://news.sina.com.cn/
                    )            [2] => Array
                    (
                        [0] => 凤凰卫视新闻
                        [1] => http://news.phoenixtv.com/
                    )            [3] => Array
                    (
                        [0] => 关注健康
                        [1] => http://www.39.net/
                    )            [4] => Array
                    (
                        [0] => 

                    )        )    [3] => Array
            (
                [0] => Array
                    (
                        [0] => 太平洋
                        [1] => http://www.XXXX.com/
                    )            [1] => Array
                    (
                        [0] => 泡泡
                        [1] => http://www.YYYY.com/
                    )            [2] => 
            ))
      

  5.   

    感谢hnxxwyq(独自流浪)问题已经解决,真的很感激~!!!!!!!!!