我需要使用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>
我得到一个错误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
DOM文档可以复制节点的结构,所以,而不必个别地复制所有的细节(其可以是容易丢失的数据,当规范的变化),则可以复制整个节点(例如<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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句