Solved

How do I parse XML with PHP not using SimpleXML

Posted on 2009-07-10
4
208 Views
Last Modified: 2012-05-07
So I'm pretty new to both php and xml, but I am trying to get some data into a database from a medical database.  Most of the data is easily parsed and I have no problem entering it into the database, but there is one xml field that looks as follows:

<MeshHeadingList>
    <MeshHeading>
        <DescriptorName MajorTopicYN="N">Animal</DescriptorName>
    </MeshHeading>
    <MeshHeading>
        <DescriptorName MajorTopicYN="N">Dogs</DescriptorName>
    </MeshHeading>
    <MeshHeading>
        <DescriptorName MajorTopicYN="Y">Myocardial Contraction</DescriptorName>
    </MeshHeading>
    <MeshHeading>
        <DescriptorName MajorTopicYN="N">Myocardium</DescriptorName>
        <QualifierName MajorTopicYN="Y">metabolism</QualifierName>
    </MeshHeading>
    <MeshHeading>
        <DescriptorName MajorTopicYN="Y">Oxygen Consumption</DescriptorName>
    </MeshHeading>
    <MeshHeading>
        <DescriptorName MajorTopicYN="N">Surface Tension</DescriptorName>
    </MeshHeading>
    </MeshHeadingList>

A lot of different headings, all with attributes.  What I have been using so far is:

$rows[$count]->meshid = $results['PubmedArticleSet']['PubmedArticle'][$count]['MeshHeadingList']['MeshHeading']['DescriptorName'];

(changed for each variable I want ), and it works with the previous one-element, non-attributed fields.  

What would be an appropriate way to somehow make the results an array that i can store?  Is there an easy way to get around the attribute field?  I don't need to store the "MajorTopicYN" or the result at all, just the key words.

Any help would be appreciated,

Thanks!

ccarlisleiv

0
Comment
Question by:ccarlisleiv
  • 3
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24826725
What is preventing you from using SimpleXML?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24827012
This shows how I might go about extracting the information from the XML, but it uses SimpleXML.  Maybe if I understood why you do not want to use SimpleXML I can show you an alternative.  Best, ~Ray
<?php // RAY_temp_xml_43.php

error_reporting(E_ALL);
 

// READABILITY

echo "<pre>\n";
 

// DATA FROM THE ORIGINAL POST WITH XML TAG PREPENDED

$xml  = '<?xml version="1.0" encoding="ISO-8859-1"?>';

$xml .= '<MeshHeadingList>

    <MeshHeading>

        <DescriptorName MajorTopicYN="N">Animal</DescriptorName>

    </MeshHeading>

    <MeshHeading>

        <DescriptorName MajorTopicYN="N">Dogs</DescriptorName>

    </MeshHeading>

    <MeshHeading>

        <DescriptorName MajorTopicYN="Y">Myocardial Contraction</DescriptorName>

    </MeshHeading>

    <MeshHeading>

        <DescriptorName MajorTopicYN="N">Myocardium</DescriptorName>

        <QualifierName MajorTopicYN="Y">metabolism</QualifierName>

    </MeshHeading>

    <MeshHeading>

        <DescriptorName MajorTopicYN="Y">Oxygen Consumption</DescriptorName>

    </MeshHeading>

    <MeshHeading>

        <DescriptorName MajorTopicYN="N">Surface Tension</DescriptorName>

    </MeshHeading>

    </MeshHeadingList>';
 

// MAKE AN OBJECT

$obj = SimpleXML_Load_String($xml);
 

// VISUALIZE THE OBJECT

var_dump($obj);
 

// ITERATE OVER THE OBJECT

foreach ($obj->MeshHeading as $mh)

{

// GET THE CONTENTS OF TAGS

    $dn = $mh->DescriptorName;

    $qn = $mh->QualifierName;

// GET THE CONTENTS OF TAG ATTRIBUTES

    $dt = isset($dn["MajorTopicYN"]) ? $dn["MajorTopicYN"] : 'NOT SET';

    $qt = isset($qn["MajorTopicYN"]) ? $qn["MajorTopicYN"] : 'NOT SET';

// SHOW THE DATA WE FOUND

    echo "\n";

    echo "\nDescriptorName:$dn ";

    echo "\nQualifierName:$qn ";

    echo "\nDescriptorName-MajorTopicYN:$dt ";

    echo "\nQualifierName-MajorTopicYN:$qt ";

    echo "\n";

}

Open in new window

0
 

Author Comment

by:ccarlisleiv
ID: 24827387
Thanks.  I realize now that I could of course use SimpleXML, but for some reason I had thought that if I used it I would have to use it for the entire document.  Thanks, and this helped a lot.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24827661
Thanks for the points!  Best of luck with your project, ~Ray
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

930 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

11 Experts available now in Live!

Get 1:1 Help Now