本帖最后由 colalc0117 于 2009-07-29 11:28:41 编辑

解决方案 »

  1.   

    $doc = new DOMDocument(1.0);
    $doc -> loadHTMLFile('XmlDomHtml.html');
    $head = $doc-> getElementsByTagName('head');
    $body = $doc-> getElementsByTagName('body');
    getTitle($head);
    parseBody($body);function getTitle($head){
    foreach ($head as $header){
    if($header->tagName == 'title'){
    echo ("网页标题:{$header->textContent}<br />");
    }
    }
    }function parseBody($Body){
    $bodyTag = $Body-> item(0);
    foreach ($bodyTag->childNodes as $element){
    $content=htmlspecialchars($element->textContent);
    switch ($element->tagName){
    case 'h2':
    echo "标题2:{$content}<br />";
    break;
    case 'p':
    echo "段落:{$content}<br />";
    break;
    case 'form':
    foreach ($element->childNodes as $input){
    if ($input->nodeType != XML_ELEMENT_NODE){
    continue; 
    }
    if ($input->tagName == 'select'){
    parseSelect($input);
    }
    }
    break;
    default:
    echo $content->tagName.'<br />';
    break;
    }
    }
    }function parseSelect($select){
    echo "下拉选择菜单:<ul>";
    $options = $select->childNodes;
    foreach ($options as $option){
    $content = htmlspecialchars($option->textContent);
    echo "<li>{$content}";
    if ($option->hasAttribute('selected')){
    echo ' <<<默认选择项';
    }
    echo "</li>";
    }
    echo '</ul>';
    }巧了 昨天刚刚做的实验
      

  2.   

    <?php
    //预先准备的数组,可以从数据库中抽取出来
    $array = array(
    array ('title'=>'Google',
    'link'=>'http://www.google.com/',
    'desc'=>'全球第一大搜索引擎,社我其谁!'
    ),
    array(
    'title'=>'Baidu',
    'link' =>'http://www.baidu.com/',
    'desc' =>'中国最大的搜索引擎,谁与争锋!'
    )
    );
    header('content-Type: application/xml;charset=utf-8');
    //显示XML的内容
    $xml = records_to_xml($array,"expense");
    echo $xml;function records_to_xml($array, $xmlname){
    $xml .= '<?xml version="1.0" encoding="utf-8" ?>' . "\n";
    $xml .= "<$xmlname>\n";
    foreach ($array as $key => $subarray){
    foreach ($subarray as $k => $v){
    $xml .= "<$k>$v</$k>\n";
    }
    }
    $xml.="</$xmlname>\n";
    return $xml;
    }?>
      

  3.   

    这段代码是杜江那本书上的 昨天刚刚做了这个方面的实验 你可以按照第二种方式生成XML代码