Solved

How do I parse XML with PHP not using SimpleXML

Posted on 2009-07-10
4
210 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 109

Expert Comment

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

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 109

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

856 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