get element by id and get element by Value xpath in php - 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"/>

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>

php update node value which has attribute

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');

Can't create element in Xml file with Php

I'm working on some project and i have to save movie(id,title,genre) in a xml file using php.
when i create xml file and add information it works perfectly
I mean ,
file : php
$document_xml = new DomDocument("1.0","UTF-8");
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$id);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$document_xml->appendChild($movie_dom);
$document_xml->save("data.xml");
and i have
file : data.xml
<?xml version="1.0" encoding="UTF-8"?>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
but when i modify the code like this
file : php
$document_xml = new DomDocument();
$document_xml->load("data.xml");
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$id);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$document_xml->appendChild($movie_dom);
$document_xml->saveXML();
so that to have,
<?xml version="1.0" encoding="UTF-8"?>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
<movie>
<id>id</id>
<title>some title</title>
<genre>some genre</genre>
</movie>
I have no error and there is no change in my xml file
I would like your help
I found solution, i change the structure of my xml file
<?xml version="1.0" encoding="UTF-8"?>
<data>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
<movie>
<id>2</id>
<title>La guerre des etoiles</title>
<genre>adventure</genre>
</movie>
<movie>
<id>3</id>
<title>Jugement dernier</title>
<genre>action</genre>
</movie>
</data>
i change my php file like this
$document_xml = new DomDocument();
$document_xml->load("data.xml",LIBXML_NOBLANKS);
$document_xml->formatOutput = true;
$root = $document_xml->documentElement;
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$value);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$root->appendChild($movie_dom);
$document_xml->appendChild($root);
$document_xml->save("data.xml");
everything wworks perfectfly ...

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