Solved

How do I parse XML with PHP not using SimpleXML

Posted on 2009-07-10
4
207 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
Comment Utility
What is preventing you from using SimpleXML?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the points!  Best of luck with your project, ~Ray
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

763 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

12 Experts available now in Live!

Get 1:1 Help Now