Categories: How ToMagento 2

How to Create SimpleXMLElement 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("/(]*>)/", "$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!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

17 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

4 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

6 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

6 days ago