Copy XML attributes PHP(复制XML属性PHP)
本文介绍了复制XML属性PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要处理XML文档,并且需要将一些字段复制到另一个XML文件。 我有此字段:
<cast>
<person name="Nanda Anand" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000"/>
<person name="Lynn Collins" character="" job="Actor" id="21044" thumb="http://cf2.imgobject.com/t/p/w185/berS11tKvXqTFThUWAYrH279cvn.jpg" department="Actors" url="http://www.themoviedb.org/person/21044" order="0" cast_id="1001"/>
</cast>
我需要将所有元素复制到另一个XML,但带有属性数据。 我使用此代码,但它不起作用:
foreach ($movies->movies->movie->cast->children() as $person)
{
$person = $cast->appendChild(
$xmldoc->createElement(("person"), str_replace("&", "&",$person)));
}
推荐答案
如果要操作xml文档(即将节点从一个文档添加到另一个文档),则应使用DOMDocument,而不是SimpleXML。
下面是使用DOMDocument从一个文档复制到另一个文档的一些代码。请注意,这是一个两个步骤的过程。首先,将节点作为$ndTemp导入到文档中。其次,将导入的$ndTemp附加到指定的父节点(我只使用根DocentElement,但它可以是另一个节点)。
注意:如果您只是在做简单的复制,您可能想要考虑使用XSL,但这是另一回事...
输入XML(Movie.xml)
<xml>
<movie name='first'>
<cast>
<person name="Nanda Anand" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000"/>
<person name="Lynn Collins" character="" job="Actor" id="21044" thumb="http://cf2.imgobject.com/t/p/w185/berS11tKvXqTFThUWAYrH279cvn.jpg" department="Actors" url="http://www.themoviedb.org/person/21044" order="0" cast_id="1001"/>
</cast>
</movie>
<movie name='Second'>
<cast>
<person name="Zaphod Beeblebrox" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000"/>
</cast>
</movie>
</xml>
PHP
<?php
$xml = new DOMDocument();
$strFileName = "movie.xml";
$xml->load($strFileName);
$xmlCopy = new DOMDocument();
$xmlCopy->loadXML( "<xml/>" );
$xpath = new domxpath( $xml );
$strXPath = "/xml/movie/cast/person";
$elements = $xpath->query( $strXPath, $xml );
foreach( $elements as $element ) {
$ndTemp = $xmlCopy->importNode( $element, true );
$xmlCopy->documentElement->appendChild( $ndTemp );
}
echo $xmlCopy->saveXML();
?>
输出
<?xml version="1.0"?>
<xml>
<person name="Nanda Anand" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000" />
<person name="Lynn Collins" character="" job="Actor" id="21044" thumb="http://cf2.imgobject.com/t/p/w185/berS11tKvXqTFThUWAYrH279cvn.jpg" department="Actors"
url="http://www.themoviedb.org/person/21044" order="0" cast_id="1001" />
<person name="Zaphod Beeblebrox" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000" />
</xml>
这篇关于复制XML属性PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:复制XML属性PHP


基础教程推荐
猜你喜欢
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01