php - 获取 xml 文件以查找和替换文本。PHP
问题描述
我需要使用 PHP 代码更改 XML 文件中的文本。然后我创建了一个代码:
1-获取文件
2-替换文本
3-用其他名称保存文件。
问题是我在替换 xml 文件中的某些文本时遇到了一些问题。
我可以替换简单的字符串,但不能用“<”之类的字符替换文本。下面是真实的代码和文件。
原始 XML 路径:http ://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml
1) 此代码只是将文本更改Inmuebles
为xxxxxxxx
. 这工作正常
$xml_external_path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml';
$xml = file_get_contents($xml_external_path);
$response = strtr($xml, array(
'Inmuebles' => 'xxxxxxxx'
));
$newXml = $response;
$newXml = simplexml_load_string( $newXml );
$newXml->asXml('/home/csainmobiliaria/www/pisos-NEW.xml');
2) 现在,如果我使用此代码将文本更改<Table Name="Inmuebles">
为<xxxxxxxx>
ERROR 500。
$xml_external_path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml';
$xml = file_get_contents($xml_external_path);
$response = strtr($xml, array(
'<Table Name="Inmuebles">' => '<xxxxxxxx>'
));
$newXml = $response;
$newXml = simplexml_load_string( $newXml );
$newXml->asXml('/home/csainmobiliaria/www/pisos-NEW.xml');
3)以同样的方式,如果我使用此代码删除文本Publicacion
,我会得到 ERROR 500。
$xml_external_path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml';
$xml = file_get_contents($xml_external_path);
$response = strtr($xml, array(
'<Publicacion>' => ''
));
$newXml = $response;
$newXml = simplexml_load_string( $newXml );
$newXml->asXml('/home/csainmobiliaria/www/pisos-NEW.xml');
这是我需要得到的最终结果:http ://www.csainmobiliaria.com/imagenes/fotos/pisos-OK.xml
解决方案
您可以复制必要的节点而不是删除任何多余的元素。例如,您可以Inmuebles
使用 SimpleXML 帮助复制节点:
$path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml';
$content = file_get_contents($path);
$sourceXML = new SimpleXMLElement($content);
$targetXML = new SimpleXMLElement("<Inmuebles></Inmuebles>");
$items = $sourceXML->xpath('Table[@Name=\'Inmuebles\']');
foreach ($items as $item) {
foreach ($item->Inmueble as $inmueble) {
$node = $targetXML->addChild('Inmueble');
$node->addChild('IdInmobiliariaExterna', $inmueble->IdInmobiliariaExterna);
$node->addChild('IdPisoExterno', $inmueble->IdPisoExterno);
$node->addChild('FechaHoraModificado', $inmueble->FechaHoraModificado);
$node->addChild('TipoInmueble', $inmueble->TipoInmueble);
$node->addChild('TipoOperacion', $inmueble->TipoOperacion);
}
}
echo $targetXML->asXML()
此外,正如@ThW 在评论中所说,您可以使用XLST,例如:
$path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml';
$content = file_get_contents($path);
$sourceXML = new SimpleXMLElement($content);
$xslt='<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Table[@Name=\'Inmuebles\']">
<Inmuebles>
<xsl:copy-of select="node()"/>
</Inmuebles>
</xsl:template>
<xsl:template match="Table[@Name=\'Agencias\']"/>
</xsl:stylesheet>';
$xsl = new SimpleXMLElement($xslt);
$processor = new XSLTProcessor;
$processor->importStyleSheet($xsl);
$result = $processor->transformToXML($sourceXML);
$targetXML = new SimpleXMLElement($result);
echo $targetXML->asXML();
推荐阅读
- react-native - 获取图标以使用 Vue native / React Native - createMaterialBottomTabNavigator
- sql - 尝试将值插入 sql 时如何避免以下错误?
- c# - 将 Web 服务 API 中的 XML 字符串反序列化为 C# 对象
- java - 如何添加选项以在 Javadoc 注释中显示 @tags
- sql - 与用户之前的订单相比,如何计算订单的相似度?
- haskell - 如何声明两种类型的组合是一个monad
- javascript - 如何让函数接受多种文件类型?
- apache - 在通配符 vhost apache (xampp) 设置中重定向子子域
- javascript - 如何访问我的复选框组件状态?
- r - R - stat_compare_means 从 Kruskal-Wallis 测试返回不同的值