一个xml形如:
<root>
<home>
<morealbum>
<cur_w>2</cur_w>
<cur_h>3</cur_h>
<cur_x>4</cur_x>
<cur_y>5</cur_y>
<cur_z>6</cur_z><title>7</title>
<introduce>hehe</introduce>
</morealbum>
</home>
</root>
要把<home>下的全部节点及内容它插入到
<root userid='111'>
<home_2>
<morealbum>
<cur_w>2</cur_w>
<cur_h>3</cur_h>
<cur_x>4</cur_x>
<cur_y>5</cur_y>
<cur_z>6</cur_z><title>7</title>
<introduce>hehe</introduce>
</morealbum>
</home_2>
</root>
的root下,请问该怎么做呢?

解决方案 »

  1.   

    一个是ajax发过来的数据,一个是xml文件中的数据,想动态的!呵呵
      

  2.   

    ajax发过来的?ajax好像只能收数据,还能发?
    你是想把ajax的返回的数据增加加到一个XML文件里吧?
      

  3.   

    直接插就好了,这没啥的。把ajax传过来的数据创建成一个DOM对象,提取相关内容,然后再打开需要修改的DOM,改了就是了
      

  4.   

    ajax是有个方法
    getResponseXML()的
      

  5.   

    $xmlDocRead->loadXML($GLOBALS['HTTP_RAW_POST_DATA']);//接受ajax传过来的xml数据并创建DOM对象
    $xmlDocWrite->loadXML(file_get_contents('8.xml'));//读取文件并创建dom对象
    $needData = $xmlDocRead->getElementsByTagName('home')->item(0);
    $xmlDocWrite->appendChild($needData);
    对吧?但是不行呀 报Uncaught exception 'DOMException' with message 'Wrong Document Error'错误!
      

  6.   

    服务器端已有 2.xml<root userid='111'> 
    <home_2> 
    <morealbum> 
    <cur_w>2 </cur_w> 
    <cur_h>3 </cur_h> 
    <cur_x>4 </cur_x> 
    <cur_y>5 </cur_y> 
    <cur_z>6 </cur_z> <title>7 </title> 
    <introduce>hehe </introduce> 
    </morealbum> 
    </home_2> 
    </root> 
    模拟传入 1.xml
    <root> 
    <home> 
    <morealbum> 
    <cur_w>2 </cur_w> 
    <cur_h>3 </cur_h> 
    <cur_x>4 </cur_x> 
    <cur_y>5 </cur_y> 
    <cur_z>6 </cur_z> <title>7 </title> 
    <introduce>hehe </introduce> 
    </morealbum> 
    </home> 
    </root> 测试程序
    $doc = new DOMDocument();
    $doc->load('2.xml'); // 打开服务器端xml
           
    $xmlPost = new DOMDocument();
    $xmlPost->loadXML(file_get_contents('1.xml')); // 模拟接收传来的xml
           
    $newNode = $doc->importNode($xmlPost->getElementsByTagName('home')->item(0),true);
    $doc->documentElement->appendChild($newNode);
       
    echo $doc->saveXML(); 
    结果
    <?xml version="1.0"?>
    <root userid="111"> 
    <home_2> 
    <morealbum> 
    <cur_w>2 </cur_w> 
    <cur_h>3 </cur_h> 
    <cur_x>4 </cur_x> 
    <cur_y>5 </cur_y> 
    <cur_z>6 </cur_z> <title>7 </title> 
    <introduce>hehe </introduce> 
    </morealbum> 
    </home_2> 
    <home> 
    <morealbum> 
    <cur_w>2 </cur_w> 
    <cur_h>3 </cur_h> 
    <cur_x>4 </cur_x> 
    <cur_y>5 </cur_y> 
    <cur_z>6 </cur_z> <title>7 </title> 
    <introduce>hehe </introduce> 
    </morealbum> 
    </home></root>