Solved

How do I parse XML with PHP not using SimpleXML

Posted on 2009-07-10
4
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

Expert Comment

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

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 110

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 …

726 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