Ad alanları ile XML PHP'nin SimpleXML kullanarak düğümü ekleme

1 Cevap php
<?xml version="1.0" encoding="ISO-8859-2"?>
    <!DOCTYPE pasaz:Envelope SYSTEM "loadOffers.dtd">
    <pasaz:Envelope xmlns:pasaz="http://schemas.xmlsoap.org/soap/envelope/">
        <pasaz:Body>
            <loadOffers xmlns="urn:ExportB2B">
                <offers />
            </loadOffers>
        </pasaz:Body>
    </pasaz:Envelope>

Ben "teklifler" düğüm bazı alt düğümleri eklemek ettik ve ben SimpleXML kullanıyorum.

PHP kodu: $offer = $xml->offers->addChild('offer') bir hata döndürür.

Ben SimpleXML elleçleme ad alanları ile sorun var, çünkü hepsi yanlış! Lütfen yardım edin!

1 Cevap

Örneğin XPath kullanarak, hedef / üst öğe olsun.

<?php
$envelope = new SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-2"?>
<!DOCTYPE pasaz:Envelope SYSTEM "loadOffers.dtd">
<pasaz:Envelope xmlns:pasaz="http://schemas.xmlsoap.org/soap/envelope/">
  <pasaz:Body>
    <loadOffers xmlns="urn:ExportB2B">
      <offers />
    </loadOffers>
  </pasaz:Body>
</pasaz:Envelope>');

$envelope->registerXPathNamespace('pasaz', 'http://schemas.xmlsoap.org/soap/envelope/');
$envelope->registerXPathNamespace('b2b', 'urn:ExportB2B');
$ns = $envelope->xpath('//pasaz:Body/b2b:loadOffers/b2b:offers');
if ( 0<count($ns) ) {
  $offers = $ns[0];
  $offers->a = 'abc';
  $offers->x = 'xyz';
}
echo $envelope->asXml();

baskılar

<?xml version="1.0" encoding="ISO-8859-2"?>
<!DOCTYPE pasaz:Envelope SYSTEM "loadOffers.dtd">
<pasaz:Envelope xmlns:pasaz="http://schemas.xmlsoap.org/soap/envelope/">
  <pasaz:Body>
    <loadOffers xmlns="urn:ExportB2B">
      <offers><a>abc</a><x>xyz</x></offers>
    </loadOffers>
  </pasaz:Body>
</pasaz:Envelope>