php update node value which has attribute - php

Hi I need to modify xml using php from exsiting xml content
existing xml
<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" BookId="10010">
<STRING id="Name" >Test1</STRING>
<STRING id="ISBN">102399</STRING>
</Book>
new xml
<?xml version="1.0" encoding="utf-8"?>
<Book BookId="10010" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<STRING id="Name" >XYZ</STRING>
<STRING id="ISBN">7777</STRING>
</Book>
Note: BookID should be place before xmlns attribute
Thank you in advance.

Use SimpleXML:
$xml = simplexml_load_file($yourXMLPath);
//Loop for element...
foreach ($xml->STRING as $string) {
//Update value for <STRING id="Name">...
if($string->attributes()->id == 'Name'){
$xml->STRING = 'XYZ';
}
//Update value for <STRING id="Name">...
if($string->attributes()->id == 'ISBN'){
$xml->STRING = '7777';
}
}
// save the updated document
$xml->asXML('newXML.xml');

Related

Add single element/single tag to XML structure with PHP Dom

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>

SimpleXML doesn't show the value

I am wondering why the below code doesn't show the value of processResponse tag while it shows the whole XML and the Body tag.
This is the XML which I am handling
$xml = '<?xml version="1.0"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>
<wsa:MessageID>urn:df1231asfer5e4564affds</wsa:MessageID>
<wsa:ReplyTo><wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo>
</env:Header>
<env:Body>
<processResponse xmlns="http://xmlns.oracle.com/EligibilityProcess/EligibilityProcess/EligibilityBPEL">
<generatedMessageRefNo>451</generatedMessageRefNo>
<providerRefNo>41</providerRefNo>
<tpaRequestId>4184612387</tpaRequestId>
<contractHolder>Rami Zbeeb</contractHolder>
<contractNo>81456954</contractNo>
<guarantorName>ANC</guarantorName>
<eligibilityStatus>Success</eligibilityStatus>
<eligibilityReason xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<messageOrNotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<patientShare></patientShare>
<consentForm></consentForm>
<webServTechStatus>Success</webServTechStatus>
<replyCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<replyDescription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</processResponse></env:Body></env:Envelope>';
I am using the SimpleXML class:
$res = new SimpleXMLElement($xml);
When I am showing the body XML it works:
$str = $res->children('env',true)->Body->asXML();
echo "<pre>",htmlentities($str),"</pre>";
However when showing the processResponse XML or string it doesn't work:
$str = $res->children('env',true)->Body->processResponse->asXML();
echo "<pre>",htmlentities($str),"</pre>";
Kindly advice.
You can get the children of Body to get the processResponse:
$str = $res->children('env',true)->Body->children()->processResponse->asXML();
echo "<pre>",htmlentities($str),"</pre>";

get element by id and get element by Value xpath in php

XML file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ew-language id="en">
<global>
<phrase id="actiondeleted" value="Deleted">
<child_phrase_1 id="1234" value="numbers">
<child id="test_id" value="test_value"/>
<child id="test_id" value="test_value_2"/>
</child_phrase_1>
</phrase>
</global>
</ew-language>
how to get element by ID and by value so the element is unique.I tried these
$parent = ($xpath->query("//*[#id='$previous_tag_id']")&& $xpath->query("//*[#value='$previous_tag_value']"))->item(0);
----------------------AND THIS ONE-----------------------------------
$parent = $xpath->query("//*[#id='$previous_tag_id']/*#value='$previous_tag_value')"->item(0);
----------------------AND THIS ONE-----------------------------------
$xpath->query("//*[#id='$previous_tag_id' and #value='$previous_tag_value']");
each syntax is not working.
Using //*[#id='$previous_tag_id' and #value='$previous_tag_value'] should've worked, see the demo in eval.in :
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ew-language id="en">
<global>
<phrase id="actiondeleted" value="Deleted">
<child_phrase_1 id="1234" value="numbers">
<child id="test_id" value="test_value"/>
<child id="test_id" value="test_value_2"/>
</child_phrase_1>
</phrase>
</global>
</ew-language>
XML;
$doc = new DOMDocument;
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$previous_tag_id = "test_id";
$previous_tag_value = "test_value_2";
$result = $xpath->query("//*[#id='$previous_tag_id' and #value='$previous_tag_value']");
foreach($result as $r)
{
echo $doc->saveXML($r);
}
output :
<child id="test_id" value="test_value_2"/>

parsing xml with PHP simplexml_load_string

so i have this XML: the result of var_dump
string '
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
<entry key="message Type">
<![
CDATA[
urgent
]
]></entry><entry key="message Title"><![
CDATA[
teswt
]
]></entry><entry key="message Body"><![
CDATA[
teasd
]
]></entry><entry key="message Priority"><![
CDATA[
1
]
]></entry></data>
' (length=260)
my code looks like this:
$xml = simplexml_load_string($tablerow['data']);
$inputs = $xml->xpath("/schema/user_input[#type!='hidden']/input|/schema/input");
foreach($inputs as $cur_input) {
$cur_input_name = (string)$cur_input['name'];
$cur_input_value = $task_input_values[$cur_input_name];
$input_name = isset($cur_input['label']) ? (string)$cur_input['label'] : $cur_input['name'];
}
var_dump($tablerow['data']);
$tablerow['data'] is the XML from the DB. any help how to get the message Title value from this??
Thanks!
The XPath expression you are using doesn't even resemble the XML document you posted, so I don't know how you would expect to ever find the values you are looking for. The following will extract "teswt" from your sample XML document:
<?php
$xml = simplexml_load_string($tablerow['data']);
$title = '';
foreach ($xml->entry as $entry) {
if ((string) $entry['key'] == 'message Title') {
$title = (string) $entry;
break;
}
}
echo $title;
?>
Edit: Updated to use the SimpleXML "style" rather than XPath.
Your XML should be in proper way.
Check This
<?php
$xmlString = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
<entry key="message Type">
<![CDATA[urgent]]>
</entry>
<entry key="message Title">
<![CDATA[teswt]]>
</entry>
<entry key="message Body">
<![CDATA[xxxxx]]>
</entry>
<entry key="message Priority">
<![CDATA[1]]>
</entry>
</data>';
$xml = simplexml_load_string($xmlString);
foreach ($xml->entry as $entry)
{
echo $entry['key']."<br/>";
}
?>

get xmlSchema of xml?

I have the file.xml contains:
<?xml version="1.0"?>
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:test.001.001.01">
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</Document>
anybody know How Can I get this information in the <document....> example the xmlns?
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:test.001.001.01">
Use SimpleXML to get namespaces like so:
$xmlstr = <<<XML
<?xml version="1.0"?>
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:test.001.001.01">
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</Document>
XML;
$xml = simplexml_load_string($xmlstr);
$namespaces = $xml->getDocNamespaces();
var_export($namespaces);
this code will produce:
array ( 'xsd' => 'http://www.w3.org/2001/XMLSchema', 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance', '' => 'urn:iso:std:iso:20022:tech:xsd:test.001.001.01', )
The array of all namespaces declared in the document.

Categories