- Change Tag Attribute Value with PHP Domdocument
- How to user DOMElement to replace attribute value
- PHP DOMDocument remove tag attribute
- Modify XML attribute PHP DOM
- PHP DOMDocument: Get attribute value from id
- In PHP, how can I use the DOMDocument class to replace the src= attribute of an IMG tag?
- DOMDocument add attribute to root tag
- Lorem'; $dom = new \DOMDocument(); $dom->loadHTML($content); $node = $dom->getElementsByTagName('h1')->item(0); $node->setAttribute('data-custom','true'); print $dom->saveHTML($node); // Lorem Alternatively, as it’s well-formed, treat the content as XML to avoid extra HTML tags being added: $content = 'Lorem'; $dom = new \DOMDocument(); $dom->loadXML($content); $dom->documentElement->setAttribute('data-custom','true'); print $dom->saveXML($dom->documentElement); // Lorem Источник How do I change XML tag names with PHP? There is some sample code that works in my question over here, but there is no direct way of changing a tag name through DOMDocument/DOMElement, you can however copy elements with a new tagname as shown. function renameTag(DOMElement $oldTag, $newTagName) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); foreach($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach($oldTag->childNodes as $child) < $newTag->appendChild($oldTag->removeChild($child)); > $oldTag->parentNode->replaceChild($newTag, $oldTag); return $newTag; > There are two issues with Kris and dfsq code: Only first child node will be copied — solved with temporary copy of $childNodes) Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document A corrected renaming function is: function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; > Rafał Lindemann 283 More Answer How to change image permission with PHP and chmod? how to rewrite the url in php with dynamic names based on id in the url how to check if xml tag has id attribute using php How to load xml file with php when CDATA isn’t valid How to change background with php and css how to create xml document on a remote sever using php with utf-8 encoding In Notepad++, how can I set «Highlight Tag Matching» to work with PHP Syntax endif, endforeach; etc.? How to change weird PHP URL with htaccess code? How to make form for uploading dozens of photos and rename them with certain names with php (flat file) How to change a file inside apk and change cert.sf and manifest.sf without java but with php How to make form for uploading dozens of photos and rename them with certain names with php (flat file) How to change a file inside apk and change cert.sf and manifest.sf without java but with php Parsing XML page with same tag names How do I add a link into an xml tag using php How to display value with field names from Solr XML files, getting values only how to read xml file in array with php How to replace a special xml tag by using php php file_get_contents on 3rd party xml with a broken tag how to find a particular tag in a string, get a value from it and then replace the string with PHP Remove closing tag XML root with PHP How to make small change to large text file in PHP without having to deal with the whole file? How do I parse an XML response with PHP that has namespaces? How can I change the user owner so a folder created with a PHP script can be edited and/or deleted through FTP? how to fetch all xml array with php loop Show XML tag full path with php change xml attribute with php How do I auto increment an xml file with php PHP Soap — How to remove xsi:type= from XML tag Selecting Node with same names in XML with PHP — Confused How to write XML file with PHP and MySql (Will it get rewritten everytime the script runs??) how to use tag with php mysql_real_escape_string with database How to read xml top 10 record (last update) with PHP How to change the value of an xml item using php How to change the value of an xml item using php how to edit specific child of xml with php How to change php database value with event click How to change PHP variable on change with JQuery How to create a link tag dynamically with onclick event attribute in php PHP — how can I echo tag with an src attribute? Change Array Key Names with PHP How can we change this PHP function to work with PHP 8? How to put the xml header, with »How to remove table, tr, td tag in html with php How to delete a node searched by attribute with its child nodes in xml by php how to make a new php array with merge fields and change name attributes PHP How can find a string start with an end tag to get the string from inside? More answer with same ag best way to split a base64 encoded pdf in PHP Search database while I enter data in to a text area Image color changed with imagecreate in php server X brings back 200 response, Server Y throws and Internal server error How to put data into database using php xcode, no errors with security in info.plist How to return a class variable as a reference (&/ampersand) in PHP? preg_replace dont replace values How do see if a domain is forwarded properly I need to splice an array at a certian key then add all values before the key to the end of a new array php Why am I getting: Call to a member function fetch_assoc() on a non-object for this recursive function? span class based on database data PHP and mysql Ajax not sending multiple php variable PHP How to extract extended data from kml file shell_exec() function is behaving strangely Display a variable based on text input and selections in php Disallowing proxies from POSTing Serialize a pthreads Stackable object Xpath regex and preg-match function Update database query Chat app using IOS and Java php name and surname validation transforming flat php array into multidimensional one based on key value? PHP question about global variables and form requests AJAX code not working on button click Sending email from localhost is not working Method chaining with the same method FusionCharts PHP with Ajax Script looking for string in file breaks Barclays ePDQ and PHP — Handling Post? zabbix configuration with rails and nginx server Weird behavior of apache2 on Ubuntu with php.ini file php modules doesn’t work in wampserver configuring symfony Run javascript from php file Prepending a string to a Smarty variable MongoDB/PHP: search for string with query PHP Regex — Remove text from HTML Tags Extension PHP with C. Creating the first environment I got awk syntax error using ext_skel Wordpress & Fancybox — Loop issue? — fancybox keeps reloading page I can’t send an email php to Hotmail? Syntax problem when checking for a file How do I push value into Angular defined variable from PHP? PHP regex to include everything before and after a domain name Can’t retrieve PDO results outside of function Get image url from BigCommerce API with PHP problems after upgrading xampp to 1.7.7 Data URI saved JPG images create black images on upload with php imagecreatefromjpeg Symfony 2 form builder custom options available in twig Sending a JQuery var into php var PHP Use include with whitespaces? Error by passing my data with JSON to a DataBase Источник How do I change XML tag names with PHP? Is there a simple way to change the tag names , to something else such as model, price. Essentially, I have a bunch of XML files containing similar data, but in different formats, so I’m looking for a simple way to parse the XML file, change certain tag names, and write a new XML file with the changed tag names. Ryan People also ask If you run the XML file as a PHP script, then yes. The output of the script will be valid XML, but the file that contains the inline PHP will most likely not be valid XML. Save this answer. Custom tags use XML syntax and are what page authors use. This is an umbrella term that describes the set of classes and conventions that allow programmers to create custom actions and tags within JSP. The tag extension mechanism was added to JSP in version 1.1. The XML document must have start-tag, so first starting tag is known as root tag. The opening tag started with < bracket followed by tag name or element name and close with >bracket. 2 Answers There are two issues with Kris and dfsq code: Only first child node will be copied — solved with temporary copy of $childNodes) Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document A corrected renaming function is: function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; > answered Oct 24 ’22 05:10 Rafał Lindemann Next function will do the trick: /** * @param $xml string Your XML * @param $old string Name of the old tag * @param $new string Name of the new tag * @return string New XML */ function renameTags($xml, $old, $new) < $dom = new DOMDocument(); $dom->loadXML($xml); $nodes = $dom->getElementsByTagName($old); $toRemove = array(); foreach ($nodes as $node) < $newNode = $dom->createElement($new); foreach ($node->attributes as $attribute) < $newNode->setAttribute($attribute->name, $attribute->value); > foreach ($node->childNodes as $child) < $newNode->appendChild($node->removeChild($child)); > $node->parentNode->appendChild($newNode); $toRemove[] = $node; > foreach ($toRemove as $node) < $node->parentNode->removeChild($node); > return $dom->saveXML(); > // Load XML from file data.xml $xml = file_get_contents('data.xml'); $xml = renameTags($xml, 'modelNumber', 'number'); $xml = renameTags($xml, 'salePrice', 'price'); echo '
'; print_r(htmlspecialchars($xml)); echo '
';
Источник - Lorem Alternatively, as it’s well-formed, treat the content as XML to avoid extra HTML tags being added: $content = 'Lorem'; $dom = new \DOMDocument(); $dom->loadXML($content); $dom->documentElement->setAttribute('data-custom','true'); print $dom->saveXML($dom->documentElement); // Lorem Источник How do I change XML tag names with PHP? There is some sample code that works in my question over here, but there is no direct way of changing a tag name through DOMDocument/DOMElement, you can however copy elements with a new tagname as shown. function renameTag(DOMElement $oldTag, $newTagName) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); foreach($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach($oldTag->childNodes as $child) < $newTag->appendChild($oldTag->removeChild($child)); > $oldTag->parentNode->replaceChild($newTag, $oldTag); return $newTag; > There are two issues with Kris and dfsq code: Only first child node will be copied — solved with temporary copy of $childNodes) Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document A corrected renaming function is: function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; > Rafał Lindemann 283 More Answer How to change image permission with PHP and chmod? how to rewrite the url in php with dynamic names based on id in the url how to check if xml tag has id attribute using php How to load xml file with php when CDATA isn’t valid How to change background with php and css how to create xml document on a remote sever using php with utf-8 encoding In Notepad++, how can I set «Highlight Tag Matching» to work with PHP Syntax endif, endforeach; etc.? How to change weird PHP URL with htaccess code? How to make form for uploading dozens of photos and rename them with certain names with php (flat file) How to change a file inside apk and change cert.sf and manifest.sf without java but with php How to make form for uploading dozens of photos and rename them with certain names with php (flat file) How to change a file inside apk and change cert.sf and manifest.sf without java but with php Parsing XML page with same tag names How do I add a link into an xml tag using php How to display value with field names from Solr XML files, getting values only how to read xml file in array with php How to replace a special xml tag by using php php file_get_contents on 3rd party xml with a broken tag how to find a particular tag in a string, get a value from it and then replace the string with PHP Remove closing tag XML root with PHP How to make small change to large text file in PHP without having to deal with the whole file? How do I parse an XML response with PHP that has namespaces? How can I change the user owner so a folder created with a PHP script can be edited and/or deleted through FTP? how to fetch all xml array with php loop Show XML tag full path with php change xml attribute with php How do I auto increment an xml file with php PHP Soap — How to remove xsi:type= from XML tag Selecting Node with same names in XML with PHP — Confused How to write XML file with PHP and MySql (Will it get rewritten everytime the script runs??) how to use tag with php mysql_real_escape_string with database How to read xml top 10 record (last update) with PHP How to change the value of an xml item using php How to change the value of an xml item using php how to edit specific child of xml with php How to change php database value with event click How to change PHP variable on change with JQuery How to create a link tag dynamically with onclick event attribute in php PHP — how can I echo tag with an src attribute? Change Array Key Names with PHP How can we change this PHP function to work with PHP 8? How to put the xml header, with »How to remove table, tr, td tag in html with php How to delete a node searched by attribute with its child nodes in xml by php how to make a new php array with merge fields and change name attributes PHP How can find a string start with an end tag to get the string from inside? More answer with same ag best way to split a base64 encoded pdf in PHP Search database while I enter data in to a text area Image color changed with imagecreate in php server X brings back 200 response, Server Y throws and Internal server error How to put data into database using php xcode, no errors with security in info.plist How to return a class variable as a reference (&/ampersand) in PHP? preg_replace dont replace values How do see if a domain is forwarded properly I need to splice an array at a certian key then add all values before the key to the end of a new array php Why am I getting: Call to a member function fetch_assoc() on a non-object for this recursive function? span class based on database data PHP and mysql Ajax not sending multiple php variable PHP How to extract extended data from kml file shell_exec() function is behaving strangely Display a variable based on text input and selections in php Disallowing proxies from POSTing Serialize a pthreads Stackable object Xpath regex and preg-match function Update database query Chat app using IOS and Java php name and surname validation transforming flat php array into multidimensional one based on key value? PHP question about global variables and form requests AJAX code not working on button click Sending email from localhost is not working Method chaining with the same method FusionCharts PHP with Ajax Script looking for string in file breaks Barclays ePDQ and PHP — Handling Post? zabbix configuration with rails and nginx server Weird behavior of apache2 on Ubuntu with php.ini file php modules doesn’t work in wampserver configuring symfony Run javascript from php file Prepending a string to a Smarty variable MongoDB/PHP: search for string with query PHP Regex — Remove text from HTML Tags Extension PHP with C. Creating the first environment I got awk syntax error using ext_skel Wordpress & Fancybox — Loop issue? — fancybox keeps reloading page I can’t send an email php to Hotmail? Syntax problem when checking for a file How do I push value into Angular defined variable from PHP? PHP regex to include everything before and after a domain name Can’t retrieve PDO results outside of function Get image url from BigCommerce API with PHP problems after upgrading xampp to 1.7.7 Data URI saved JPG images create black images on upload with php imagecreatefromjpeg Symfony 2 form builder custom options available in twig Sending a JQuery var into php var PHP Use include with whitespaces? Error by passing my data with JSON to a DataBase Источник How do I change XML tag names with PHP? Is there a simple way to change the tag names , to something else such as model, price. Essentially, I have a bunch of XML files containing similar data, but in different formats, so I’m looking for a simple way to parse the XML file, change certain tag names, and write a new XML file with the changed tag names. Ryan People also ask If you run the XML file as a PHP script, then yes. The output of the script will be valid XML, but the file that contains the inline PHP will most likely not be valid XML. Save this answer. Custom tags use XML syntax and are what page authors use. This is an umbrella term that describes the set of classes and conventions that allow programmers to create custom actions and tags within JSP. The tag extension mechanism was added to JSP in version 1.1. The XML document must have start-tag, so first starting tag is known as root tag. The opening tag started with < bracket followed by tag name or element name and close with >bracket. 2 Answers There are two issues with Kris and dfsq code: Only first child node will be copied — solved with temporary copy of $childNodes) Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document A corrected renaming function is: function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; > answered Oct 24 ’22 05:10 Rafał Lindemann Next function will do the trick: /** * @param $xml string Your XML * @param $old string Name of the old tag * @param $new string Name of the new tag * @return string New XML */ function renameTags($xml, $old, $new) < $dom = new DOMDocument(); $dom->loadXML($xml); $nodes = $dom->getElementsByTagName($old); $toRemove = array(); foreach ($nodes as $node) < $newNode = $dom->createElement($new); foreach ($node->attributes as $attribute) < $newNode->setAttribute($attribute->name, $attribute->value); > foreach ($node->childNodes as $child) < $newNode->appendChild($node->removeChild($child)); > $node->parentNode->appendChild($newNode); $toRemove[] = $node; > foreach ($toRemove as $node) < $node->parentNode->removeChild($node); > return $dom->saveXML(); > // Load XML from file data.xml $xml = file_get_contents('data.xml'); $xml = renameTags($xml, 'modelNumber', 'number'); $xml = renameTags($xml, 'salePrice', 'price'); echo '
'; print_r(htmlspecialchars($xml)); echo '
';
Источник - Lorem'; $dom = new \DOMDocument(); $dom->loadXML($content); $dom->documentElement->setAttribute('data-custom','true'); print $dom->saveXML($dom->documentElement); // Lorem Источник How do I change XML tag names with PHP? There is some sample code that works in my question over here, but there is no direct way of changing a tag name through DOMDocument/DOMElement, you can however copy elements with a new tagname as shown. function renameTag(DOMElement $oldTag, $newTagName) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); foreach($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach($oldTag->childNodes as $child) < $newTag->appendChild($oldTag->removeChild($child)); > $oldTag->parentNode->replaceChild($newTag, $oldTag); return $newTag; > There are two issues with Kris and dfsq code: Only first child node will be copied — solved with temporary copy of $childNodes) Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document A corrected renaming function is: function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; > Rafał Lindemann 283 More Answer How to change image permission with PHP and chmod? how to rewrite the url in php with dynamic names based on id in the url how to check if xml tag has id attribute using php How to load xml file with php when CDATA isn’t valid How to change background with php and css how to create xml document on a remote sever using php with utf-8 encoding In Notepad++, how can I set «Highlight Tag Matching» to work with PHP Syntax endif, endforeach; etc.? How to change weird PHP URL with htaccess code? How to make form for uploading dozens of photos and rename them with certain names with php (flat file) How to change a file inside apk and change cert.sf and manifest.sf without java but with php How to make form for uploading dozens of photos and rename them with certain names with php (flat file) How to change a file inside apk and change cert.sf and manifest.sf without java but with php Parsing XML page with same tag names How do I add a link into an xml tag using php How to display value with field names from Solr XML files, getting values only how to read xml file in array with php How to replace a special xml tag by using php php file_get_contents on 3rd party xml with a broken tag how to find a particular tag in a string, get a value from it and then replace the string with PHP Remove closing tag XML root with PHP How to make small change to large text file in PHP without having to deal with the whole file? How do I parse an XML response with PHP that has namespaces? How can I change the user owner so a folder created with a PHP script can be edited and/or deleted through FTP? how to fetch all xml array with php loop Show XML tag full path with php change xml attribute with php How do I auto increment an xml file with php PHP Soap — How to remove xsi:type= from XML tag Selecting Node with same names in XML with PHP — Confused How to write XML file with PHP and MySql (Will it get rewritten everytime the script runs??) how to use tag with php mysql_real_escape_string with database How to read xml top 10 record (last update) with PHP How to change the value of an xml item using php How to change the value of an xml item using php how to edit specific child of xml with php How to change php database value with event click How to change PHP variable on change with JQuery How to create a link tag dynamically with onclick event attribute in php PHP — how can I echo tag with an src attribute? Change Array Key Names with PHP How can we change this PHP function to work with PHP 8? How to put the xml header, with »How to remove table, tr, td tag in html with php How to delete a node searched by attribute with its child nodes in xml by php how to make a new php array with merge fields and change name attributes PHP How can find a string start with an end tag to get the string from inside? More answer with same ag best way to split a base64 encoded pdf in PHP Search database while I enter data in to a text area Image color changed with imagecreate in php server X brings back 200 response, Server Y throws and Internal server error How to put data into database using php xcode, no errors with security in info.plist How to return a class variable as a reference (&/ampersand) in PHP? preg_replace dont replace values How do see if a domain is forwarded properly I need to splice an array at a certian key then add all values before the key to the end of a new array php Why am I getting: Call to a member function fetch_assoc() on a non-object for this recursive function? span class based on database data PHP and mysql Ajax not sending multiple php variable PHP How to extract extended data from kml file shell_exec() function is behaving strangely Display a variable based on text input and selections in php Disallowing proxies from POSTing Serialize a pthreads Stackable object Xpath regex and preg-match function Update database query Chat app using IOS and Java php name and surname validation transforming flat php array into multidimensional one based on key value? PHP question about global variables and form requests AJAX code not working on button click Sending email from localhost is not working Method chaining with the same method FusionCharts PHP with Ajax Script looking for string in file breaks Barclays ePDQ and PHP — Handling Post? zabbix configuration with rails and nginx server Weird behavior of apache2 on Ubuntu with php.ini file php modules doesn’t work in wampserver configuring symfony Run javascript from php file Prepending a string to a Smarty variable MongoDB/PHP: search for string with query PHP Regex — Remove text from HTML Tags Extension PHP with C. Creating the first environment I got awk syntax error using ext_skel Wordpress & Fancybox — Loop issue? — fancybox keeps reloading page I can’t send an email php to Hotmail? Syntax problem when checking for a file How do I push value into Angular defined variable from PHP? PHP regex to include everything before and after a domain name Can’t retrieve PDO results outside of function Get image url from BigCommerce API with PHP problems after upgrading xampp to 1.7.7 Data URI saved JPG images create black images on upload with php imagecreatefromjpeg Symfony 2 form builder custom options available in twig Sending a JQuery var into php var PHP Use include with whitespaces? Error by passing my data with JSON to a DataBase Источник How do I change XML tag names with PHP? Is there a simple way to change the tag names , to something else such as model, price. Essentially, I have a bunch of XML files containing similar data, but in different formats, so I’m looking for a simple way to parse the XML file, change certain tag names, and write a new XML file with the changed tag names. Ryan People also ask If you run the XML file as a PHP script, then yes. The output of the script will be valid XML, but the file that contains the inline PHP will most likely not be valid XML. Save this answer. Custom tags use XML syntax and are what page authors use. This is an umbrella term that describes the set of classes and conventions that allow programmers to create custom actions and tags within JSP. The tag extension mechanism was added to JSP in version 1.1. The XML document must have start-tag, so first starting tag is known as root tag. The opening tag started with < bracket followed by tag name or element name and close with >bracket. 2 Answers There are two issues with Kris and dfsq code: Only first child node will be copied — solved with temporary copy of $childNodes) Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document A corrected renaming function is: function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; > answered Oct 24 ’22 05:10 Rafał Lindemann Next function will do the trick: /** * @param $xml string Your XML * @param $old string Name of the old tag * @param $new string Name of the new tag * @return string New XML */ function renameTags($xml, $old, $new) < $dom = new DOMDocument(); $dom->loadXML($xml); $nodes = $dom->getElementsByTagName($old); $toRemove = array(); foreach ($nodes as $node) < $newNode = $dom->createElement($new); foreach ($node->attributes as $attribute) < $newNode->setAttribute($attribute->name, $attribute->value); > foreach ($node->childNodes as $child) < $newNode->appendChild($node->removeChild($child)); > $node->parentNode->appendChild($newNode); $toRemove[] = $node; > foreach ($toRemove as $node) < $node->parentNode->removeChild($node); > return $dom->saveXML(); > // Load XML from file data.xml $xml = file_get_contents('data.xml'); $xml = renameTags($xml, 'modelNumber', 'number'); $xml = renameTags($xml, 'salePrice', 'price'); echo '
'; print_r(htmlspecialchars($xml)); echo '
';
Источник - Lorem Источник How do I change XML tag names with PHP? There is some sample code that works in my question over here, but there is no direct way of changing a tag name through DOMDocument/DOMElement, you can however copy elements with a new tagname as shown. function renameTag(DOMElement $oldTag, $newTagName) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); foreach($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach($oldTag->childNodes as $child) < $newTag->appendChild($oldTag->removeChild($child)); > $oldTag->parentNode->replaceChild($newTag, $oldTag); return $newTag; > There are two issues with Kris and dfsq code: Only first child node will be copied — solved with temporary copy of $childNodes) Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document A corrected renaming function is: function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; > Rafał Lindemann 283 More Answer How to change image permission with PHP and chmod? how to rewrite the url in php with dynamic names based on id in the url how to check if xml tag has id attribute using php How to load xml file with php when CDATA isn’t valid How to change background with php and css how to create xml document on a remote sever using php with utf-8 encoding In Notepad++, how can I set «Highlight Tag Matching» to work with PHP Syntax endif, endforeach; etc.? How to change weird PHP URL with htaccess code? How to make form for uploading dozens of photos and rename them with certain names with php (flat file) How to change a file inside apk and change cert.sf and manifest.sf without java but with php How to make form for uploading dozens of photos and rename them with certain names with php (flat file) How to change a file inside apk and change cert.sf and manifest.sf without java but with php Parsing XML page with same tag names How do I add a link into an xml tag using php How to display value with field names from Solr XML files, getting values only how to read xml file in array with php How to replace a special xml tag by using php php file_get_contents on 3rd party xml with a broken tag how to find a particular tag in a string, get a value from it and then replace the string with PHP Remove closing tag XML root with PHP How to make small change to large text file in PHP without having to deal with the whole file? How do I parse an XML response with PHP that has namespaces? How can I change the user owner so a folder created with a PHP script can be edited and/or deleted through FTP? how to fetch all xml array with php loop Show XML tag full path with php change xml attribute with php How do I auto increment an xml file with php PHP Soap — How to remove xsi:type= from XML tag Selecting Node with same names in XML with PHP — Confused How to write XML file with PHP and MySql (Will it get rewritten everytime the script runs??) how to use tag with php mysql_real_escape_string with database How to read xml top 10 record (last update) with PHP How to change the value of an xml item using php How to change the value of an xml item using php how to edit specific child of xml with php How to change php database value with event click How to change PHP variable on change with JQuery How to create a link tag dynamically with onclick event attribute in php PHP — how can I echo tag with an src attribute? Change Array Key Names with PHP How can we change this PHP function to work with PHP 8? How to put the xml header, with »How to remove table, tr, td tag in html with php How to delete a node searched by attribute with its child nodes in xml by php how to make a new php array with merge fields and change name attributes PHP How can find a string start with an end tag to get the string from inside? More answer with same ag best way to split a base64 encoded pdf in PHP Search database while I enter data in to a text area Image color changed with imagecreate in php server X brings back 200 response, Server Y throws and Internal server error How to put data into database using php xcode, no errors with security in info.plist How to return a class variable as a reference (&/ampersand) in PHP? preg_replace dont replace values How do see if a domain is forwarded properly I need to splice an array at a certian key then add all values before the key to the end of a new array php Why am I getting: Call to a member function fetch_assoc() on a non-object for this recursive function? span class based on database data PHP and mysql Ajax not sending multiple php variable PHP How to extract extended data from kml file shell_exec() function is behaving strangely Display a variable based on text input and selections in php Disallowing proxies from POSTing Serialize a pthreads Stackable object Xpath regex and preg-match function Update database query Chat app using IOS and Java php name and surname validation transforming flat php array into multidimensional one based on key value? PHP question about global variables and form requests AJAX code not working on button click Sending email from localhost is not working Method chaining with the same method FusionCharts PHP with Ajax Script looking for string in file breaks Barclays ePDQ and PHP — Handling Post? zabbix configuration with rails and nginx server Weird behavior of apache2 on Ubuntu with php.ini file php modules doesn’t work in wampserver configuring symfony Run javascript from php file Prepending a string to a Smarty variable MongoDB/PHP: search for string with query PHP Regex — Remove text from HTML Tags Extension PHP with C. Creating the first environment I got awk syntax error using ext_skel Wordpress & Fancybox — Loop issue? — fancybox keeps reloading page I can’t send an email php to Hotmail? Syntax problem when checking for a file How do I push value into Angular defined variable from PHP? PHP regex to include everything before and after a domain name Can’t retrieve PDO results outside of function Get image url from BigCommerce API with PHP problems after upgrading xampp to 1.7.7 Data URI saved JPG images create black images on upload with php imagecreatefromjpeg Symfony 2 form builder custom options available in twig Sending a JQuery var into php var PHP Use include with whitespaces? Error by passing my data with JSON to a DataBase Источник How do I change XML tag names with PHP? Is there a simple way to change the tag names , to something else such as model, price. Essentially, I have a bunch of XML files containing similar data, but in different formats, so I’m looking for a simple way to parse the XML file, change certain tag names, and write a new XML file with the changed tag names. Ryan People also ask If you run the XML file as a PHP script, then yes. The output of the script will be valid XML, but the file that contains the inline PHP will most likely not be valid XML. Save this answer. Custom tags use XML syntax and are what page authors use. This is an umbrella term that describes the set of classes and conventions that allow programmers to create custom actions and tags within JSP. The tag extension mechanism was added to JSP in version 1.1. The XML document must have start-tag, so first starting tag is known as root tag. The opening tag started with < bracket followed by tag name or element name and close with >bracket. 2 Answers There are two issues with Kris and dfsq code: Only first child node will be copied — solved with temporary copy of $childNodes) Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document A corrected renaming function is: function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; > answered Oct 24 ’22 05:10 Rafał Lindemann Next function will do the trick: /** * @param $xml string Your XML * @param $old string Name of the old tag * @param $new string Name of the new tag * @return string New XML */ function renameTags($xml, $old, $new) < $dom = new DOMDocument(); $dom->loadXML($xml); $nodes = $dom->getElementsByTagName($old); $toRemove = array(); foreach ($nodes as $node) < $newNode = $dom->createElement($new); foreach ($node->attributes as $attribute) < $newNode->setAttribute($attribute->name, $attribute->value); > foreach ($node->childNodes as $child) < $newNode->appendChild($node->removeChild($child)); > $node->parentNode->appendChild($newNode); $toRemove[] = $node; > foreach ($toRemove as $node) < $node->parentNode->removeChild($node); > return $dom->saveXML(); > // Load XML from file data.xml $xml = file_get_contents('data.xml'); $xml = renameTags($xml, 'modelNumber', 'number'); $xml = renameTags($xml, 'salePrice', 'price'); echo '
'; print_r(htmlspecialchars($xml)); echo '
';
Источник - How do I change XML tag names with PHP?
- More Answer
- More answer with same ag
- How do I change XML tag names with PHP?
- Ryan
- People also ask
- 2 Answers
- Rafał Lindemann
Change Tag Attribute Value with PHP Domdocument
Ensure the media namespace is defined in the returned XML otherwise DOMDocument will error out.
If you are getting a specific error, please edit your post to include it
$xmldoc = new DOMDocument();
$xmldoc->load('api response address');
foreach ($xmldoc->getElementsByTagName('item') as $feeditem) $nodes = $feeditem->getElementsByTagName('file');
$linkthumb = $nodes->item(0)->getAttribute('data');
echo $linkthumb;
>
You may also want to look at SimpleXML and Xpath as it makes reading XML much easier than DOMDocument.
How to user DOMElement to replace attribute value
PHP is different from jQuery in a lot of very significant ways, but to answer your question:
$domelement->setAttribute("href", "http://www.google.se");
You might want to consult the documentation.
PHP DOMDocument remove tag attribute
Probably xml:lang=»en» is not corrected value of attribute in html. Change these lines:
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED);
//code.
echo $dom->saveHTML($dom->documentElement);
$dom->loadXML($content, LIBXML_HTML_NOIMPLIED);
//code.
echo $dom->saveXML($dom->documentElement);
Modify XML attribute PHP DOM
Not sure what you want to do exactly, but the general idea is :
- You must instanciate DOMDocument
- and load your XML strings with it : DOMDocument::loadXML
- Then, you must instanciate DOMXpath on that document
- And use it to query the document : DOMXPath::query
- One you have found the node that interests you, you can manipulate it
Here, for example, you could use some thing like this :
$str =
XML;
$dom = new DOMDocument();
$dom->loadXML($str);
$xpath = new DOMXPath($dom);
$elements = $xpath->query('//record[@id="my record"]');
if ($elements->length >= 1) $element = $elements->item(0);
$element->setAttribute('id', "glop !");
>
echo '' . htmlspecialchars($dom->saveXML()) . '';
This will replace the id attribute my record , on the node that’s identified by it, by » glop ! «, and you’d get the following XML as output :
PHP DOMDocument: Get attribute value from id
$attrs = array();
for ($i = 0; $i < $data->attributes->length; ++$i) $node = $data->attributes->item($i);
$attrs[$node->nodeName] = $node->nodeValue;
>
var_dump($attrs);In PHP, how can I use the DOMDocument class to replace the src= attribute of an IMG tag?
This is what I’ve come up with. It uses the PHP DOM API to create a tiny HTML document, then saves the XML for just the IMG element.
function replace_img_src($original_img_tag, $new_src_url) $doc = new DOMDocument();
$doc->loadHTML($original_img_tag);
$tags = $doc->getElementsByTagName('img');
if(count($tags) > 0)
$tag = $tags->item(0);
$tag->setAttribute('src', $new_src_url);
return $doc->saveHTML($tag);
>
return false;
>Note: In versions of PHP before 5.3.6, $doc->saveHTML($tag) can be changed to $doc->saveXML($tag) .
DOMDocument add attribute to root tag
When you output the HTML, select a specific node rather than the whole document:
$content = 'Lorem';
$dom = new \DOMDocument();
$dom->loadHTML($content);
$node = $dom->getElementsByTagName('h1')->item(0);
$node->setAttribute('data-custom','true');
print $dom->saveHTML($node);
//Lorem
Alternatively, as it’s well-formed, treat the content as XML to avoid extra HTML tags being added:
$content = 'Lorem';
$dom = new \DOMDocument();
$dom->loadXML($content);
$dom->documentElement->setAttribute('data-custom','true');
print $dom->saveXML($dom->documentElement);
//Lorem
How do I change XML tag names with PHP?
There is some sample code that works in my question over here, but there is no direct way of changing a tag name through DOMDocument/DOMElement, you can however copy elements with a new tagname as shown.
function renameTag(DOMElement $oldTag, $newTagName) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); foreach($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach($oldTag->childNodes as $child) < $newTag->appendChild($oldTag->removeChild($child)); > $oldTag->parentNode->replaceChild($newTag, $oldTag); return $newTag; >
There are two issues with Kris and dfsq code:
- Only first child node will be copied — solved with temporary copy of $childNodes)
- Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document
A corrected renaming function is:
function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; >
Rafał Lindemann 283
More Answer
- How to change image permission with PHP and chmod?
- how to rewrite the url in php with dynamic names based on id in the url
- how to check if xml tag has id attribute using php
- How to load xml file with php when CDATA isn’t valid
- How to change background with php and css
- how to create xml document on a remote sever using php with utf-8 encoding
- In Notepad++, how can I set «Highlight Tag Matching» to work with PHP Syntax endif, endforeach; etc.?
- How to change weird PHP URL with htaccess code?
- How to make form for uploading dozens of photos and rename them with certain names with php (flat file)
- How to change a file inside apk and change cert.sf and manifest.sf without java but with php
- How to make form for uploading dozens of photos and rename them with certain names with php (flat file)
- How to change a file inside apk and change cert.sf and manifest.sf without java but with php
- Parsing XML page with same tag names
- How do I add a link into an xml tag using php
- How to display value with field names from Solr XML files, getting values only
- how to read xml file in array with php
- How to replace a special xml tag by using php
- php file_get_contents on 3rd party xml with a broken tag
- how to find a particular tag in a string, get a value from it and then replace the string with PHP
- Remove closing tag XML root with PHP
- How to make small change to large text file in PHP without having to deal with the whole file?
- How do I parse an XML response with PHP that has namespaces?
- How can I change the user owner so a folder created with a PHP script can be edited and/or deleted through FTP?
- how to fetch all xml array with php loop
- Show XML tag full path with php
- change xml attribute with php
- How do I auto increment an xml file with php
- PHP Soap — How to remove xsi:type= from XML tag
- Selecting Node with same names in XML with PHP — Confused
- How to write XML file with PHP and MySql (Will it get rewritten everytime the script runs??)
- how to use tag with php mysql_real_escape_string with database
- How to read xml top 10 record (last update) with PHP
- How to change the value of an xml item using php
- How to change the value of an xml item using php
- how to edit specific child of xml with php
- How to change php database value with event click
- How to change PHP variable on change with JQuery
- How to create a link tag dynamically with onclick event attribute in php
- PHP — how can I echo tag with an src attribute?
- Change Array Key Names with PHP
- How can we change this PHP function to work with PHP 8?
- How to put the xml header, with »
- How to remove table, tr, td tag in html with php
- How to delete a node searched by attribute with its child nodes in xml by php
- how to make a new php array with merge fields and change name attributes
- PHP How can find a string start with an end tag to get the string from inside?
More answer with same ag
- best way to split a base64 encoded pdf in PHP
- Search database while I enter data in to a text area
- Image color changed with imagecreate in php
- server X brings back 200 response, Server Y throws and Internal server error
- How to put data into database using php xcode, no errors with security in info.plist
- How to return a class variable as a reference (&/ampersand) in PHP?
- preg_replace dont replace values
- How do see if a domain is forwarded properly
- I need to splice an array at a certian key then add all values before the key to the end of a new array php
- Why am I getting: Call to a member function fetch_assoc() on a non-object for this recursive function?
- span class based on database data PHP and mysql
- Ajax not sending multiple php variable
- PHP How to extract extended data from kml file
- shell_exec() function is behaving strangely
- Display a variable based on text input and selections in php
- Disallowing proxies from POSTing
- Serialize a pthreads Stackable object
- Xpath regex and preg-match function
- Update database query
- Chat app using IOS and Java
- php name and surname validation
- transforming flat php array into multidimensional one based on key value?
- PHP question about global variables and form requests
- AJAX code not working on button click
- Sending email from localhost is not working
- Method chaining with the same method
- FusionCharts PHP with Ajax
- Script looking for string in file breaks
- Barclays ePDQ and PHP — Handling Post?
- zabbix configuration with rails and nginx server
- Weird behavior of apache2 on Ubuntu with php.ini file
- php modules doesn’t work in wampserver configuring symfony
- Run javascript from php file
- Prepending a string to a Smarty variable
- MongoDB/PHP: search for string with query
- PHP Regex — Remove text from HTML Tags
- Extension PHP with C. Creating the first environment I got awk syntax error using ext_skel
- WordPress & Fancybox — Loop issue? — fancybox keeps reloading page
- I can’t send an email php to Hotmail?
- Syntax problem when checking for a file
- How do I push value into Angular defined variable from PHP?
- PHP regex to include everything before and after a domain name
- Can’t retrieve PDO results outside of function
- Get image url from BigCommerce API with PHP
- problems after upgrading xampp to 1.7.7
- Data URI saved JPG images create black images on upload with php imagecreatefromjpeg
- Symfony 2 form builder custom options available in twig
- Sending a JQuery var into php var
- PHP Use include with whitespaces?
- Error by passing my data with JSON to a DataBase
How do I change XML tag names with PHP?
Is there a simple way to change the tag names , to something else such as model, price.
Essentially, I have a bunch of XML files containing similar data, but in different formats, so I’m looking for a simple way to parse the XML file, change certain tag names, and write a new XML file with the changed tag names.
Ryan
People also ask
If you run the XML file as a PHP script, then yes. The output of the script will be valid XML, but the file that contains the inline PHP will most likely not be valid XML. Save this answer.
Custom tags use XML syntax and are what page authors use. This is an umbrella term that describes the set of classes and conventions that allow programmers to create custom actions and tags within JSP. The tag extension mechanism was added to JSP in version 1.1.
The XML document must have start-tag, so first starting tag is known as root tag. The opening tag started with < bracket followed by tag name or element name and close with >bracket.
2 Answers
There are two issues with Kris and dfsq code:
- Only first child node will be copied — solved with temporary copy of $childNodes)
- Children will get xmlns tag — solved by replacing node at the beginning — so it’s connected to the document
A corrected renaming function is:
function renameTag( DOMElement $oldTag, $newTagName ) < $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) < $newTag->setAttribute($attribute->name, $attribute->value); > foreach (iterator_to_array($oldTag->childNodes) as $child) < $newTag->appendChild($oldTag->removeChild($child)); > return $newTag; >
answered Oct 24 ’22 05:10
Rafał Lindemann
Next function will do the trick:
/** * @param $xml string Your XML * @param $old string Name of the old tag * @param $new string Name of the new tag * @return string New XML */ function renameTags($xml, $old, $new) < $dom = new DOMDocument(); $dom->loadXML($xml); $nodes = $dom->getElementsByTagName($old); $toRemove = array(); foreach ($nodes as $node) < $newNode = $dom->createElement($new); foreach ($node->attributes as $attribute) < $newNode->setAttribute($attribute->name, $attribute->value); > foreach ($node->childNodes as $child) < $newNode->appendChild($node->removeChild($child)); > $node->parentNode->appendChild($newNode); $toRemove[] = $node; > foreach ($toRemove as $node) < $node->parentNode->removeChild($node); > return $dom->saveXML(); > // Load XML from file data.xml $xml = file_get_contents('data.xml'); $xml = renameTags($xml, 'modelNumber', 'number'); $xml = renameTags($xml, 'salePrice', 'price'); echo ''; print_r(htmlspecialchars($xml)); echo '
';