I have the following code
$base = '<?xml version="1.0" encoding="UTF-8"?><realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink"></realestates:office>';
$objXml = new \SimpleXMLElement($base);
$objXml->addChild('title', 'Alles Toller');
$strXml = $objXml->asXML();
$strXml would now be
<?xml version="1.0" encoding="UTF-8"?>
<realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<realestates:title>Alles Toller</realestates:title>
</realestates:office>
What I want is to not have the realestates: prefix in <title>
<?xml version="1.0" encoding="UTF-8"?>
<realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Alles Toller</title>
</realestates:office>
How can I accomplish that?
I ended up in using DOMDocument:
$objXml = new \DOMDocument( "1.0", "UTF-8" );
$objRoot = $objXml->createElement('realestates:office');
$objXml->appendChild($objRoot);
$objXml->createAttributeNS('http://rest.immobilienscout24.de/schema/offer/realestates/1.0', 'realestates:office');
$objXml->createAttributeNS('http://www.w3.org/1999/xlink', 'xlink:dummy');
$objTitle = $objXml->createElement('title', 'Alles Toll');
$objRoot->appendChild($objTitle);
$strXml = $objXml->saveXML();
The second parameter of createAttributeNS() is a bit strange - only the namespace is not enough, it looks like I have to also add an element name.
Related
I have a question regarding addChild in SimpleXMLElement.
i try to create xml file using SimpleXMLElement with specific header as a requirement and structure like this :
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<ns1:Order xmlns:ns1=\"rrn:org.xcbl:schemas/xcbl/v3_5/xcbl35.xsd\"><node id=\"1\" name=\"node 1\"><subnode id=\"1.1\" name=\"subnode 1.1\"/><subnode id=\"1.2\" name=\"subnode 1.2\"><inner_node id=\"1.2.1\" name=\"inner node 1.2.1\"/></subnode></node></ns1:Order>
this is my code :
$xml_header = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns1:Order xmlns:ns1="rrn:org.xcbl:schemas/xcbl/v3_5/xcbl35.xsd"></ns1:Order>';
$xml = new SimpleXMLElement($xml_header);
$node1 = $xml->addChild('node');
$node1->addAttribute('id', '1');
$node1->addAttribute('name', 'node 1');
$subnode1 = $node1->addChild('subnode');
$subnode1->addAttribute('id', '1.1');
$subnode1->addAttribute('name', 'subnode 1.1');
$subnode2 = $node1->addchild('subnode');
$subnode2->addAttribute('id', '1.2');
$subnode2->addAttribute('name', 'subnode 1.2');
$inner_node1 = $subnode2->addChild('inner_node');
$inner_node1->addAttribute('id', '1.2.1');
$inner_node1->addAttribute('name', 'inner node 1.2.1');
$this->response = $xml->asXML();
output:
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<ns1:Order xmlns:ns1=\"rrn:org.xcbl:schemas/xcbl/v3_5/xcbl35.xsd\"><ns1:node id=\"1\" name=\"node 1\"><ns1:subnode id=\"1.1\" name=\"subnode 1.1\"/><ns1:subnode id=\"1.2\" name=\"subnode 1.2\"><ns1:inner_node id=\"1.2.1\" name=\"inner node 1.2.1\"/></ns1:subnode></ns1:node></ns1:Order>
Does anyone have any idea how to adding a child without ns1 prefix inside ns1:Order tag?
I try to add a single element/single tag without a closing tag to an XML structure with PHP Dom to get something like this:
<?xml version="1.0" encoding="UTF-8"?>
<ndxml>
<credentials>
<identity>User</identity>
<password>Password</password>
<language name="default" modifier="de"/>
</credentials>
</ndxml>
Does anybody know which command I can use to get this?
$domDocument = new \DOMDocument('1.0', 'UTF-8');
$rootNode = $domDocument->createElement('ndxml');
$credentials = $domDocument->createElement('credentials');
$credentials->appendChild(new \DOMElement('identity', 'User'));
$credentials->appendChild(new \DOMElement('password', 'Password'));
$language = $domDocument->createElement('language');
$language->setAttribute('name', 'default');
$language->setAttribute('modifier', 'de');
$credentials->appendChild($language);
$rootNode->appendChild($credentials);
$domDocument->appendChild($rootNode);
echo $domDocument->saveXML();
echo "\n\n";
echo $domDocument->saveXML(null, LIBXML_NOEMPTYTAG);
OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<ndxml><credentials><identity>User</identity><password>Password</password><language name="default" modifier="de"/></credentials></ndxml>
<?xml version="1.0" encoding="UTF-8"?>
<ndxml><credentials><identity>User</identity><password>Password</password><language name="default" modifier="de"></language></credentials></ndxml>
I am trying to parse the below XML , i have tryed loads of different solutions, i have provided an example of what i have tryed. I have read the SimpleXML documents and i still cant get this right. In the Example below all im trying to do is Echo out a line in the XML.
<?php
$xmlstr = '
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SubmitLeadResponse xmlns="https://test.com/">
<SubmitLeadResult>
<Result>C</Result>
<RedirectURL>https://testred.com</RedirectURL>
<ApplicantID>123</ApplicantID>
<ConfirmedPrice>0</ConfirmedPrice>
<PotentialPrice>0</PotentialPrice>
</SubmitLeadResult>
</SubmitLeadResponse>
</soap:Body>
</soap:Envelope>'
;
?>
<?php
$SubmitLeadResponse = new SimpleXMLElement($xmlstr);
echo $SubmitLeadResponse->SubmitLeadResult[0]->RedirectURL;
?>
You can try below code for SimpleXML
<?php
$xml ='<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SubmitLeadResponse xmlns="https://test.com/">
<SubmitLeadResult>
<Result>C</Result>
<RedirectURL>https://testred.com</RedirectURL>
<ApplicantID>123</ApplicantID>
<ConfirmedPrice>0</ConfirmedPrice>
<PotentialPrice>0</PotentialPrice>
</SubmitLeadResult>
</SubmitLeadResponse>
</soap:Body>
</soap:Envelope>';
$get_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $xml);
$xml = simplexml_load_string($get_xml);
print"<pre>";
print_r((string)$xml->Body->SubmitLeadResponse->SubmitLeadResult->RedirectURL);
echo "<br /><br /><br />";
print_r($xml);
?>
I've changed your code a bit. Here's a working sample to get the RedirectURL:
<?php
$xmlstr = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SubmitLeadResponse xmlns="https://test.com/">
<SubmitLeadResult>
<Result>C</Result>
<RedirectURL>https://testred.com</RedirectURL>
<ApplicantID>123</ApplicantID>
<ConfirmedPrice>0</ConfirmedPrice>
<PotentialPrice>0</PotentialPrice>
</SubmitLeadResult>
</SubmitLeadResponse>
</soap:Body>
</soap:Envelope>';
$doc = new DOMDocument();
$doc->loadXML( $xmlstr );
$RedirectURL = $doc->getElementsByTagName( "RedirectURL" );
$RedirectURL = $LoginResults->item(0)->nodeValue;
var_dump( $RedirectURL );
Sample provided from this source: There are also more informations to reading SOAP-envelopes without a soapclient
Please note that it's good practice to omit the php closing tag and stay in the php-context as long as possible to avoid unexpected outputs (linebreaks).
Your XML contains namespaced elements, so it's a little more complicated to parse. It can be done by passing the namespace values to children() like so:
Codepad demo
$SubmitLeadResponse = new SimpleXMLElement($xmlstr);
echo (string)$SubmitLeadResponse
->children('http://schemas.xmlsoap.org/soap/envelope/')
->Body
->children('https://test.com/')
->SubmitLeadResponse
->SubmitLeadResult
->RedirectURL;
Outputs
https://testred.com
Note: SimpleXML doesn't like new lines before the XML string, so remove the new line, making it:
$xmlstr = '<?xml version="1.0" encoding="utf-8"?>
This is the result I'd like to have:
<?xml version="1.0" encoding="utf-8"?>
<types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
</types>
but using the straightforward solution:
$document = new DOMDocument('1.0', 'utf-8');
$schema = $document->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:schema');
$types = $document->createElement('types');
$types->appendChild($schema);
$document->appendChild($types);
echo $document->saveXML();
I only get this:
<?xml version="1.0" encoding="utf-8"?>
<types xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
</types>
What am I missing?
The problem is in order of appending children. Try this:
$document = new DOMDocument('1.0', 'utf-8');
$types = $document->createElement('types');
$schema = $document->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:schema');
$document->appendChild($types);
$types->appendChild($schema);
echo $document->saveXML();
<?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>
I've to add some child nodes to "offers" node and I'm using SimpleXML.
The PHP code: $offer = $xml->offers->addChild('offer') returns an error.
It's all wrong because I've got problem with handling namespaces in SimpleXML! Please help!
E.g. by using xpath the get the target/parent element.
<?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();
prints
<?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>