Solved

How do I parse XML with PHP not using SimpleXML

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

691 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