一个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下,请问该怎么做呢?
<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下,请问该怎么做呢?
你是想把ajax的返回的数据增加加到一个XML文件里吧?
getResponseXML()的
$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'错误!
<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>