Question

I hope somebody can help me with this. I have a script that generates an XML-file:

<Reeleezee version="1.15" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.reeleezee.nl/taxonomy/1.15 taxonomy\1.15\Reeleezee.xsd" xmlns="http://www.reeleezee.nl/taxonomy/1.15">
<Import>
    <CustomerList>
        <Customer>
            <ID/>
            <FullName>Dennis Rijken</FullName>
            <SearchName>Dennis Rijken</SearchName>
            <Code/>
            <DefaultAddress>Office</DefaultAddress>
            <LanguageCode>nl</LanguageCode>
            <FaxNumber/>
            <EmailAddress/>
            <WebsiteAddress/>
            <Comment/>
            <ChamberOfCommerceNumber/>
            <ChamberOfCommerceCity/>
            <FiscalIdentificationNumber/>
            <TaxDepositLHNumber/>
            <TaxDepositOBNumber/>
            <TaxDepositICLNumber/>
            <BankAccountNumber/>
            <BranchReference/>
            <AddressList>
                <Address Type="Office">
                    <Street>Stuivenes</Street>
                    <Number>11</Number>
                    <Zipcode>3911 XX</Zipcode>
                    <City>Rhenen</City>
                    <CountryCode>NL</CountryCode>
                </Address>
            </AddressList>
            <ContactPersonList>
                <ContactPersonReference/>
            </ContactPersonList>
        </Customer>
    </CustomerList>
</Import>

But it should be like:

<Customer RlzID="23232.51.8C8D54156584290">
            <ID xsi:nil="true" />
            <FullName>GPXS</FullName>
            <SearchName>GPXS</SearchName>
            <Code xsi:nil="true" />
            <DefaultAddress>Office</DefaultAddress>
            <LanguageCode>nl</LanguageCode>
            <PhoneNumber xsi:nil="true" />
            <FaxNumber xsi:nil="true" />
            <EmailAddress xsi:nil="true" />
            <WebsiteAddress xsi:nil="true" />
            <Comment xsi:nil="true" />
            <ChamberOfCommerceNumber xsi:nil="true" />
            <ChamberOfCommerceCity xsi:nil="true" />
            <FiscalIdentificationNumber xsi:nil="true" />
            <TaxDepositLHNumber xsi:nil="true" />
            <TaxDepositOBNumber xsi:nil="true" />
            <TaxDepositICLNumber xsi:nil="true" />
            <BankAccountNumber xsi:nil="true" />
            <BranchReference xsi:nil="true" />
            <AddressList>
                <Address Type="Office">
                    <Street>Stephensonstraat</Street>
                    <Number>19</Number>
                    <Zipcode>1097 BA</Zipcode>
                    <City>Amsterdam</City>
                    <CountryCode>NL</CountryCode>
                </Address>
                <Address Type="Delivery" xsi:nil="true" />
            </AddressList>
            <ContactPersonList>
                <ContactPersonReference RlzID="23232.89.8CA58ACEACD0560" />
            </ContactPersonList>
        </Customer>

The xml is generated by the PHP script is as follows:

$query = "SELECT orders.id AS orderid, DATE_FORMAT(orders.datum, '%d-%m-%Y %k:%i:%s') AS datum, orders.bedrag, debiteur.naam, debiteur.achternaam, debiteur.adres, debiteur.huisnummer, debiteur.toevoeging, debiteur.postcode, debiteur.plaats, debiteur.rek_nummer, debiteur.email
FROM orders 
LEFT JOIN debiteur ON orders.debiteur_id = debiteur.id";

$result = mysql_query($query) or die(mysql_error());

$exportXML = new SimpleXMLElement("<Reeleezee></Reeleezee>");
$import = $exportXML->addChild('Import');

 $customerlist = $import->addChild('CustomerList');

while($res = mysql_fetch_array($result, MYSQL_ASSOC)){


$customer = $customerlist->addChild('Customer');
$customer->addChild('ID');
$customer->addChild('FullName', $res['naam']." ".$res['achternaam']);
$customer->addChild('SearchName', $res['naam']." ".$res['achternaam']);
$customer->addChild('Code');
$customer->addChild('DefaultAddress', 'Office');
$customer->addChild('LanguageCode', 'nl');
$customer->addChild('FaxNumber');
$customer->addChild('EmailAddress');
$customer->addChild('WebsiteAddress');
$customer->addChild('Comment');
$customer->addChild('ChamberOfCommerceNumber');
$customer->addChild('ChamberOfCommerceCity');
$customer->addChild('FiscalIdentificationNumber');
$customer->addChild('TaxDepositLHNumber');
$customer->addChild('TaxDepositOBNumber');
$customer->addChild('TaxDepositICLNumber');
$customer->addChild('BankAccountNumber');
$customer->addChild('BranchReference');

$addresslist = $customer->addChild('AddressList');
$address = $addresslist->addChild('Address');
$address->addAttribute('Type', 'Office');
$address->addChild('Street', $res['adres']);
$address->addChild('Number', $res['huisnummer']);
$address->addChild('Zipcode', $res['postcode']);
$address->addChild('City', $res['plaats']);
$address->addChild('CountryCode', 'NL');

$contactpersonlist = $customer->addChild('ContactPersonList');
$contactpersonlist->addChild('ContactPersonReference');

}

Header('Content-type: text/xml');
$strxml = $exportXML->asXML();

$root = '<Reeleezee version='.'"1.15"'.' xmlns:xsi='.'"http://www.w3.org/2001/XMLSchema-instance"'.' xsi:schemaLocation='.'"http://www.reeleezee.nl/taxonomy/1.15 taxonomy\1.15\Reeleezee.xsd"'.' xmlns='.'"http://www.reeleezee.nl/taxonomy/1.15"'.'>';

 $newxml = str_replace("<Reeleezee>", $root, $strxml);
 //echo $newxml;
 file_put_contents("test.xml", $newxml);

How can I add the xsi:nil="true" to an XML-element ?

Can somebody please help me? And how can I define an XML-element type, like integer, string etc.

Was it helpful?

Solution

If you need an Namespace, you must write a Prefix of the Namespace + the Name of the Attribute ("prefix:name") as the first parameter AND the uri as the third one.

Example code:

 <?php
   $xml = new SimpleXMLElement("<packagedElement></packagedElement>");
   $xml->addAttribute("xsi:nil", "true", "http://www.w3.org/2001/XMLSchema-instance"); 
   echo $xml->asXml();
 ?>

Output:

<packagedElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"></packagedElement>

EDIT:

In your case :(Try this)

Change

 $customer->addChild('ID');

to

 $customer->addChild('ID')->addAttribute("xsi:nil", "true");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top