How to Create SimpleXMLElement Object in Magento 2

img how to create simple XMLElement Object in Magento 2

Working with Development & customization in Magento 2, every day we get something fresh challenges from our clients that we love to accept. Nowadays, e-Commerce Store owners are in a hunt for that one thing which delivers great shopping experience and secure transactions which Magento provides. One of our Clients also came with the requirement in which he wants to parse the XML response in Magento 2. As of now, it’s not allowed to parse data directly, so we have found one simple trick which we would like to share with our readers.

  $ch = curl_init();

  $sendurl = "http://example.com";
  curl_setopt($ch, CURLOPT_URL, $sendurl);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
  $data = curl_exec($ch);
  curl_close($ch);

  $response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $data);
  $xml = new \SimpleXMLElement($data);

To parse XML data, we have to use \SimpleXMLElement to define object of Simple XML Element. Now you know to create XML to parse data, you can improve your skills by parsing different XML feeds from various APIs. Comment down below if you face any issue while parsing data and stay tuned for more tips & tricks.
Happy Parsing!

Previous Article

How to Install Magento SUPEE 10415 With or Without SSH

Next Article

How to fix “Unique constraint violation found” in Magento 2 Checkout Page After Migration

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨