I have the following xml :
<assumption_list>
<assumption name="test" id="23" description="test1" is_shared="no">
<watchlists>
<watchlist globalissuer="koolwater" prepayrate="5" prepaytype="CPR" defaultrate="5" defaulttype="CDR" lossrate="7" lagmonths="2"/>
</watchlists>
</assumption>
</assumption_list>
I load the following received from a jsp call in php as DOMDocument
I am trying the get the <watchlists> node as a string by using the following code :
$result = $xmlDoc->getElementsByTagName('watchlists');
$strxml='';
foreach($result as $element)
{
print_r(simplexml_import_dom($element));
$strxml = $xmlDoc->saveXML($element);
var_dump($strxml);
}
I do see my print_r(simplexml_import_dom($element)); this getting populated but for some reasons i see the empty string after the saveXml operation. Is there something wrong with the current implementation.
I am trying to get the xml string representation so that i can pass the xml string to c# dll used by php application
If you want to output the xml string representation, you can use a simple htmlentities() on this one. Consider this example: Sample Output
$xmlDoc = '<assumption_list> <assumption name="test" id="23" description="test1" is_shared="no"> <watchlists> <watchlist globalissuer="koolwater" prepayrate="5" prepaytype="CPR" defaultrate="5" defaulttype="CDR" lossrate="7" lagmonths="2"/> </watchlists> </assumption></assumption_list>';
$xmlDoc = simplexml_load_string($xmlDoc);
$result = $xmlDoc->assumption->watchlists->watchlist;
// echo $result->asXML(); // output as xml
echo htmlentities($result->asXML()); // output as xml string
Related
I want to create dynamic tags in XML using PHP
like this : <wsse:Username>fqsuser01</wsse:Username>
the main thing is that I want the tags will change the value inside ---> "wsse"
(like this value)
what I need to do? to create this XML file wite PHP?
Thanks,
For this purpose you can use XMLWriter for example (another option is SimpleXML). Both option are in PHP core so any third party libraries aren't needed. wsse is a namespace - more about them you can read here
I also share with you some example code:
<?php
//create a new xmlwriter object
$xml = new XMLWriter();
//using memory for string output
$xml->openMemory();
//set the indentation to true (if false all the xml will be written on one line)
$xml->setIndent(true);
//create the document tag, you can specify the version and encoding here
$xml->startDocument();
//Create an element
$xml->startElement("root");
//Write to the element
$xml->writeElement("r1:id", "1");
$xml->writeElement("r2:id", "2");
$xml->writeElement("r3:id", "3");
$xml->endElement(); //End the element
//output the xml
echo $xml->outputMemory();
?>
Result:
<?xml version="1.0"?>
<root>
<r1:id>1</r1:id>
<r2:id>2</r2:id>
<r3:id>3</r3:id>
</root>
You could use a string and convert it to XML using simplexml_load_string(). The string must be well formed.
<?php
$usernames= array(
'username01',
'username02',
'username03'
);
$xml_string = '<wsse:Usernames>';
foreach($usernames as $username ){
$xml_string .= "<wsse:Username>$username</wsse:Username>";
}
$xml_string .= '</wsse:Usernames>';
$note=
<<<XML
$xml_string
XML; //backspace this line all the way to the left
$xml=simplexml_load_string($note);
?>
If you wanted to be able to change the namespaces on each XML element you would do something very similar to what is shown above. (Form a string with dynamic namespaces)
The XML portion that I instructed you to backspace all of the way has weird behavior. See https://www.w3schools.com/php/func_simplexml_load_string.asp for an example that you can copy & paste.
I've got an xml like this:
<father>
<son>Text with <b>HTML</b>.</son>
</father>
I'm using simplexml_load_string to parse it into SimpleXmlElement. Then I get my node like this
$xml->father->son->__toString(); //output: "Text with .", but expected "Text with <b>HTML</b>."
I need to handle simple HTML such as:
<b>text</b> or <br/> inside the xml which is sent by many users.
Me problem is that I can't just ask them to use CDATA because they won't be able to handle it properly, and they are already use to do without.
Also, if it's possible I don't want the file to be edited because the information need to be the one sent by the user.
The function simplexml_load_string simply erase anything inside HTML node and the HTML node itself.
How can I keep the information ?
SOLUTION
To handle the problem I used the asXml as explained by #ThW:
$tmp = $xml->father->son->asXml(); //<son>Text with <b>HTML</b>.</son>
I just added a preg_match to erase the node.
A CDATA section is a character node, just like a text node. But it does less encoding/decoding. This is mostly a downside, actually. On the upside something in a CDATA section might be more readable for a human and it allows for some BC in special cases. (Think HTML script tags.)
For an XML API they are nearly the same. Here is a small DOM example (SimpleXML abstracts to much).
$document = new DOMDocument();
$father = $document->appendChild(
$document->createElement('father')
);
$son = $father->appendChild(
$document->createElement('son')
);
$son->appendChild(
$document->createTextNode('With <b>HTML</b><br>It\'s so nice.')
);
$son = $father->appendChild(
$document->createElement('son')
);
$son->appendChild(
$document->createCDataSection('With <b>HTML</b><br>It\'s so nice.')
);
$document->formatOutput = TRUE;
echo $document->saveXml();
Output:
<?xml version="1.0"?>
<father>
<son>With <b>HTML</b><br>It's so nice.</son>
<son><![CDATA[With <b>HTML</b><br>It's so nice.]]></son>
</father>
As you can see they are serialized very differently - but from the API view they are basically exchangeable. If you're using an XML parser the value you get back should be the same in both cases.
So the first possibility is just letting the HTML fragment be stored in a character node. It is just a string value for the outer XML document itself.
The other way would be using XHTML. XHTML is XML compatible HTML. You can mix an match different XML formats, so you could add the XHTML fragment as part of the outer XML.
That seems to be what you're receiving. But SimpleXML has some problems with mixed nodes. So here is an example how you can read it in DOM.
$xml = <<<'XML'
<father>
<son>With <b>HTML</b><br/>It's so nice.</son>
</father>
XML;
$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$result = '';
foreach ($xpath->evaluate('/father/son[1]/node()') as $child) {
$result .= $document->saveXml($child);
}
echo $result;
Output:
With <b>HTML</b><br/>It's so nice.
Basically you need to save each child of the son element as XML.
SimpleXML is based on the same DOM library internally. That allows you to convert a SimpleXMLElement into a DOM node. From there you can again save each child as XML.
$father = new SimpleXMLElement($xml);
$sonNode = dom_import_simplexml($father->son);
$document = $sonNode->ownerDocument;
$result = '';
foreach ($sonNode->childNodes as $child) {
$result .= $document->saveXml($child);
}
echo $result;
I have got this xml structure below. For every 'locatie' (in every 'land') I need the 'id' value and it's 'sneeuwkwaliteit'.
My effort so far, does not return the value of 'sneeuwkwaliteit':
$sneeuw = simplexml_load_file('ski.xml');
echo $sneeuw->land[0]->locaties[0]->sneeuw->{'ski'}->sneeuwkwaliteit;
<sneeuw>
<aanmaak_tijd>09-01-2016 07:48</aanmaak_tijd>
<landen>
<land id="Andorra">
<locaties>
<locatie id="Arinsal/Pal">
<ski>
<datum_tijd>09-01-2016</datum_tijd>
<sneeuwhoogte_dal>40</sneeuwhoogte_dal>
<sneeuwhoogte_berg>70</sneeuwhoogte_berg>
<sneeuwkwaliteit>De aanwezige sneeuw is poedersneeuw.</sneeuwkwaliteit>
<datum_laatste_sneeuwval>07-01-2016</datum_laatste_sneeuwval>
<hoeveelheid_laatste_sneeuwval>20</hoeveelheid_laatste_sneeuwval>
<totaal_aantal_liften>25</totaal_aantal_liften>
<aantal_liften_geopend>25</aantal_liften_geopend>
</ski>
</locatie>
</locaties>
</land>
</landen>
</sneeuw>
Just carefully follow the path from the root of your XML to the target element :
$xml = <<<XML
<sneeuw>
<aanmaak_tijd>09-01-2016 07:48</aanmaak_tijd>
<landen>
<land id="Andorra">
<locaties>
<locatie id="Arinsal/Pal">
<ski>
<datum_tijd>09-01-2016</datum_tijd>
<sneeuwhoogte_dal>40</sneeuwhoogte_dal>
<sneeuwhoogte_berg>70</sneeuwhoogte_berg>
<sneeuwkwaliteit>De aanwezige sneeuw is poedersneeuw.</sneeuwkwaliteit>
<datum_laatste_sneeuwval>07-01-2016</datum_laatste_sneeuwval>
<hoeveelheid_laatste_sneeuwval>20</hoeveelheid_laatste_sneeuwval>
<totaal_aantal_liften>25</totaal_aantal_liften>
<aantal_liften_geopend>25</aantal_liften_geopend>
</ski>
</locatie>
</locaties>
</land>
</landen>
</sneeuw>
XML;
$sneeuw = simplexml_load_string($xml);
echo $sneeuw->landen[0]->land[0]->locaties[0]->locatie[0]->ski[0]->sneeuwkwaliteit;
eval.in demo
output :
De aanwezige sneeuw is poedersneeuw.
For more complex query against XML, look into XPath, which has it's own specification. Then you can execute XPath expression using SimpleXMLElement::xpath() function.
UPDATE :
You can use XPath, as mentioned above, to iterate through all sneeuwkwaliteit elements located in the same path :
....
$result = $sneeuw->xpath('/sneeuw/landen/land/locaties/locatie/ski/sneeuwkwaliteit');
foreach($result as $r){
echo $r ."<br>";
}
$result = "<QRYRESULT>
<ISSUCCESS>Y</ISSUCCESS>
<TRN_REF>2498297295729857927</TRN_REF>
<WARNING>IF ANY WARNING</WARNING>
</QRYRESULT>";
this is an XML string I am using. now I have to add a new node to this string (not a file). That is after adding the new node the XML string will look like the following. I am getting this XML string from another system in a variable so modifying the string manually is not an option.
$result = "<QRYRESULT>
<ISSUCCESS>Y</ISSUCCESS>
<TOKEN>some token</TOKEN>
<TRN_REF>2498297295729857927</TRN_REF>
<WARNING>IF ANY WARNING</WARNING>
</QRYRESULT>";
how can I do that ??
Use eg. simplexml
$xml = new SimpleXMLElement($yourstring);
$xml->addChild('node_name', 'node_value');
echo $xml->asXML();
A variant without using XML and consistency check:
$result=str_replace("</ISSUCCESS>","</ISSUCCESS><TOKEN>some token</TOKEN>",$result);
I have a PHP page that a service sends out a notice when a donation is made. My PHP page is supposed to grab the XML contents and parse it out for processing.
The service sends the following format:
http://myserver.com/myphp.com?details= xml data
I have the following code listening for this post:
//Load xml from post
$data = file_get_contents('php://input');
$xmlData = simplexml_load_string($data);
//grab mobile number to query mgive for user info
$mnumb= $xmlData->MobileNumber;
$mnumb=ltrim($mnumb,'1');
I am getting the following error when the service sends out the notice.
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in
what am I missing or coding wrong?
12-18-2013 855cst
Thanks ThW.. Progress is being made.
I used your suggestion #2 to get the data.
When I do a print_r(xmlData), I get no output on the screen. However, when using chrome developer tool, I get the following output:
data=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22%3F%3E%3CGetDonationStatusResult+xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22+xmlns%3Axsd%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%22%3E%3CResultCode+%3E0%3C%2FResultCode%3E%3CResultText+%2F%3E%3CRecordID+%3E0%3C%2FRecordID%3E%3CMobileNumber+%3E12142911111%3C%2FMobileNumber%3E%3CCarrierID+%3E31002%3C%2FCarrierID%3E%3CCarrierName+%3EAT%26amp%3BT+Wireless%3C%2FCarrierName%3E%3CDonationStatus+%3EUserAccepted%3C%2FDonationStatus%3E%3CMobileTransactionID+%3E62622731%3C%2FMobileTransactionID%3E%3CDonationMsgGUID+%3E9c17d57f-b54e-488a-8cf5-1c658d1aa618%3C%2FDonationMsgGUID%3E%3CCampaignID+%3E20409%3C%2FCampaignID%3E%3CShortCode+%3E27722%3C%2FShortCode%3E%3CMsgTime+%3E2013-12-17T12%3A53%3A18%3C%2FMsgTime%3E%3CMessageText+%3ELIBERIA+WAP%3C%2FMessageText%3E%3C%2FGetDonationStatusResult%3E&*
You can see MobileNumber is sent. But, when I echo $mnumb, I get no output. Am I missing something in my use of simplexml_load_string($data); to grab the MobileNumber?
It is not clear where you get the xml data from.
Read from the detail parameter in the url $xml = $_GET['detail'];
Read from the data parameter in the url $xml = $_GET['data'];
Read from the data parameter in the request body (post) $xml = $_POST['data'];
Read the raw post data $xml = file_get_contents("php://input");
Try to var_dump() the data:
var_dump($_GET, $_POST);
After you got the XML into a variable use DOM + Xpath to extract values from it:
$xml = <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<GetDonationStatusResult xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema">
<ResultCode >0</ResultCode>
<ResultText />
<RecordID >0</RecordID>
<MobileNumber >19191112222</MobileNumber>
<CarrierID >31002</CarrierID>
<CarrierName >AT&T Wireless</CarrierName>
<DonationStatus >UserAccepted</DonationStatus>
<MsgTime >2013-12-17T20:53:05</MsgTime>
<MessageText >LIBERIA WAP</MessageText>
</GetDonationStatusResult>
XML;
$dom = new DOMDocument();
// try to load the xml
if ($dom->loadXml($xml)) {
$xpath = new DOMXpath($dom);
// read the first MobileNumber element as string
var_dump(
$xpath->evaluate('string(//MobileNumber)')
);
}
Output:
string(11) "19191112222"
Live Result