gpt4 book ai didi

php - 获取 xml 文件以查找和替换文本。 PHP

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:07:28 26 4
gpt4 key购买 nike

我需要使用 PHP 代码更改 XML 文件中的文本。然后我创建了一个代码:

1-获取文件

2-替换文本

3- 用其他名称保存文件。

问题是我在替换 xml 文件中的某些文本时遇到了一些问题。

我可以替换简单字符串,但无法用“<”等字符替换文本。下面是真正的代码和文件。

原始 XML 路径:http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml

1) 此代码仅更改文本 Inmueblesxxxxxxxx . 这很好用

    $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> 我收到错误 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 我收到错误 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

捕获:enter image description here

最佳答案

DOMDocument允许您复制节点的结构,因此不必单独复制所有细节(规范更改时可能容易丢失数据),您可以将整个节点(例如 <Inmueble> )从一个文档复制到另一个文档使用 importNode() 其中有一个参数指示应复制元素的全部内容。这种方法还允许您使用相同的函数复制任何表而无需更改代码...

function extractData ( $sourceFile, $table )    {
// Load source data
$source = new DOMDocument();
$source->load($sourceFile);
$xp = new DOMXPath($source);

// Create new data document
$newFile = new DOMDocument();
$newFile->formatOutput = true;
// Create base element with the table name in new document
$newRoot = $newFile->createElement($table);
$newFile->appendChild($newRoot);

// Find the records to copy
$records = $xp->query('//Table[@Name="'.$table.'"]/*');
foreach ( $records as $record ) {
// Import the node to copy and append it to new document
$newRoot->appendChild();
}
// Return the source of the XML
return $newFile->saveXML();
}

echo extractData ($xml_external_path, "Inmuebles");

如果您希望进一步处理文档,您可以更改方法以将文档作为 DOMDocument 或什至是 SimpleXML 版本返回。

对于 SimpleXML,将返回更改为...

return simplexml_import_dom($newRoot);

然后你可以称它为...

$ret = extractData ($xml_external_path, "Inmuebles");
echo $ret->asXML();

或者,如果您只想以一种固定的方式执行此操作,您可以删除 XPath 并只使用 getElementsByTagName()找到要复制的节点...

$source = new DOMDocument();
$source->load($xml_external_path);

$newFile = new DOMDocument();
$newRoot = $newFile->createElement("Inmuebles");
$newFile->appendChild($newRoot);

// Find the records to copy
foreach ( $source->getElementsByTagName("Inmueble") as $record ) {
$newRoot->appendChild($newFile->importNode($record, true));
}
echo $newFile->saveXML();

为了添加保存文件名,我向函数添加了一个新参数,这个新函数根本不返回任何东西——它只是加载文件并将结果保存到新文件名...

function extractData ( $sourceFile, $table, $newFileName )    {
// Load source data
$source = new DOMDocument();
$source->load($sourceFile);
$xp = new DOMXPath($source);

// Create new file document
$newFile = new DOMDocument();
$newFile->formatOutput = true;
// Create base element with the table name in new document
$newRoot = $newFile->createElement($table);
$newFile->appendChild($newRoot);

// Find the records to copy
$records = $xp->query('//Table[@Name="'.$table.'"]/*');
foreach ( $records as $record ) {
// Import the node to copy and append it to new document
$importNode = $newFile->importNode($record, true);
// Add new content
$importNode->appendChild($newFile->createElement("Title", "value"));
$newRoot->appendChild();
}

// Update Foto elements
$xp = new DOMXPath($newFile);
$fotos = $xp->query("//*[starts-with(local-name(), 'Foto')]");
foreach ( $fotos as $foto ) {
$path = $foto->nodeValue;
if( substr($path, 0, 5) == "/www/" ) {
$path = substr($path,4);
}
// Replace node with new version
$foto->parentNode->replaceChild($newFile->createElement("Foto1", $path),
$foto);
}

$newFile->save($newFileName);
}
$xml_external_path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos.xml';
$xml_external_savepath = 'saveFile.xml';

extractData ($xml_external_path, "Inmuebles", $xml_external_savepath);

关于php - 获取 xml 文件以查找和替换文本。 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54365585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com