0
Имейте это sample.xml:SimpleXML вставки новой строки
<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", так что я tryng сделать в 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
Любые идеи ?, спасибо в продвинутом виде. Извините за мой английский.
Вы не показываете поколение '$ target', но ошибка говорит о том, что $ target должен быть простымXMLElement, а не логическим var. –
$ target - последний узел Barrio $ target = current ($ FacturacionAR-> xpath ('// Barrio [last()]')) ;, возможно, есть ошибка – user1487091
Скорее всего, вы используете устаревшую версию PHP. Также вы пропустите, чтобы включить отчет об ошибках на самый высокий уровень. PHP говорит вам уже раньше, где ваша проблема. Ваша проблема в том, что 'current()' возвращает логическое значение, то есть в случае только с некоторыми версиями PHP: http://3v4l.org/g69GQ - правильная версия PHP вернет здесь 'NULL'. Также вы должны видеть предупреждения повсюду, поэтому включите отчет об ошибках и установите отчет на самый высокий уровень. – hakre