checking for xml tag name exists in PHP

Posted on 2012-09-22
Last Modified: 2012-10-01
I want to check if certain tags exist in xml
$data = $xmlString;
$xml = simplexml_load_string($data);
//  does $xml->xxxName exist the do something
$customerName = $xml->xxxName;

Open in new window

Question by:prowebinteractiveinc
    LVL 13

    Accepted Solution

    "Certain tag" is a big vague but if the name of the tag is know ahead of time, you could do something like this (borrowed and then modified from W3schools).

    $xmlstring = <<<XML
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <body>Don't forget me this weekend!</body>
    $xml = simplexml_load_string($xmlstring);
    if ( isset ( $xml->from ))
      echo "from = $xml->from" . PHP_EOL;
      echo "from isn't set" . PHP_EOL;

    Open in new window

    I showed that for illustration (and also because I can debug that example).

    Here's what you want, assuming the tag is called name.

    $data = $xmlString;
    $xml = simplexml_load_string($data);
    if ( isset ( xml->xxxName ))
      $customerName = $xml->xxxName;

    Open in new window

    If that doesn't work, I'll need a better understanding of what you are trying to accomplish.
    LVL 107

    Expert Comment

    by:Ray Paseur
    If this is a follow-on from your earlier question that had invalid XML, you would want to start by loading the XML, then testing to see if the object existed or if the SimpleXML_Load_xxx() returned FALSE. Just a thought... ~Ray

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now