拥有此sample.xml:
<FacturacionAR>
<Factura xsi:type="FacturaAR">
<TipoNota>Boleta de Ventas y Servicios</TipoNota>
<Agencia>LUIL</Agencia>
<NroFactura>5040</NroFactura>
<Cliente>JOHAO SMITH CART</Cliente>
<Ciudad>NUNORK</Ciudad>
<Direccion>CALLE VITAL DE MELLO</Direccion>
<Barrio>JAJJKIU</Barrio>
<Estado>Cancelada</Estado>
</Factura>
</FacturacionAR>
想要在每次丢失此节点时添加“Telefono”,只需“Barrio”,所以我在php中尝试做:
$filename = "sample.xml";
$FacturacionAR = simplexml_load_file($filename,null,true);
function simplexml_insert_after(SimpleXMLElement $sxe, SimpleXMLElement $insert, SimpleXMLElement $target)
{
$target_dom = dom_import_simplexml($target);
$target_dom->formatOutput = true;
$target_dom->preserveWhiteSpace = false;
$insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
if ($target_dom->nextSibling) {
$result = $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
$target_dom->parentNode->insertBefore($target_dom->ownerDocument->createTextNode("\n"), $result);
return $result;
} else {
return $target_dom->parentNode->appendChild($insert_dom);
}
}
foreach ($FacturacionAR->Factura as $Factura) {
if (!isset($Factura->Telefono)) {
$meta2 = new SimpleXMLElement("<Telefono/>");
$target = current($FacturacionAR->xpath('//Barrio[last()]'));
simplexml_insert_after($FacturacionLocalizaAR, $meta2, $target);
}
}
预期结果是:
<Barrio>JAJJKIU</Barrio>
<Telefono></Telefono>
但是当我运行php脚本时,会出现此错误:
PHP Catchable fatal error: Argument 3 passed to simplexml_insert_after() must be an instance of SimpleXMLElement, boolean given
任何想法?,谢谢你的先进。 抱歉我的英文。
答案 0 :(得分:0)
行。升级到PHP 5.5.3解决了我的问题。