Avatar of srikanth saladi
srikanth saladi
 asked on

parsing xml having similar tags but different values

Hi

I am facing a problem with xml parsing.My xml is like this

<textlist><textitem>
<text>
disc 1
</text>
<type>
disc_title
</type>
</textitem>
<textitem>
<text>
cd
</text>
<type>
cd_title
</type>
</textitem>
<textitem>
<text>
disk asdf
</text>
<type>
disc_text
</type>
</textitem>
<textitem>
<text>
cd has ddf
</text>
<type>
cd_text
</type>
</textitem>
<textitem>
<text>
disc_img.png
</text>
<type>
disc_image
</type>
</textitem>
</textlist>

To fetch the disc related  data i have stored types in db in a table in three columns(item_title(disc_title),item_text(disc_text),item_image(disc_image)) .While parsing I want to fetching the types from db and comparing them with xml (<type>) types and read corresponding  <text> values.But the problem is the tags are not in sequence.for disc title ,text,image are not in sequence.How is it possible to read the disc info and store in an array for further processing.

eg.I want the data like item_array['title'],item_array['text'],item_array['image'].I have tried like this
$info = $xml->{'textlist'}->{'textitem'};

foreach(info  as  $item_details)
{
   if(in_array( $item_details->{'type'},$dbtablearray['item_title']))
{
$title = $item_details->{'text'};
}
 if(in_array( $item_details->{'type'},$dbtablearray['item_text']))
{
$text = $item_details->{'text'};
}
}
$item_array =array('title'=>$title ........) like this
PHPXML

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

You don't use arrays for this -- you use objects.  I'll see what I can do with your data structure and will post a code sample in a moment.
Ray Paseur

Please see http://www.laprbass.com/RAY_temp_srikanth_saladi.php
The "textitem" is an array of five objects.  In the context of this question, arrays and objects are almost 100% interchangeable.  You can use iterators like foreach() on both.  Objects have the syntactical advantage that you don't need quotes in the notation.  With objects you use notation like object->property, but with arrays you must use something like array['key'] and as a result you have some special "escape" issues if you want to put array elements into query strings or echo statements.

As you can see in the code sample below, we can extract the data elements by name, making the order of elements unimportant.

<?php // RAY_temp_srikanth_saladi.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28329900.html

// TEST DATA FROM THE POSTED QUESTION
$xml = <<<EOD
<textlist><textitem>
<text>
disc 1
</text>
<type>
disc_title
</type>
</textitem>
<textitem>
<text>
cd
</text>
<type>
cd_title
</type>
</textitem>
<textitem>
<text>
disk asdf
</text>
<type>
disc_text
</type>
</textitem>
<textitem>
<text>
cd has ddf
</text>
<type>
cd_text
</type>
</textitem>
<textitem>
<text>
disc_img.png
</text>
<type>
disc_image
</type>
</textitem>
</textlist>
EOD;

// MAKE AN OBJECT AND VISUALIZE IT (NOTE THE EXTRANEOUS END-OF-LINE CHARACTERS IN THE DISPLAY)
$obj = SimpleXML_Load_String($xml);
echo '<pre>';
var_dump($obj);

// SHOW HOW TO USE ITERATORS TO FIND THE OBJECTS INSIDE THE textitem ARRAY
foreach ($obj->textitem as $item)
{
    $text = trim($item->text);
    $type = trim($item->type);
    echo PHP_EOL . "TEXT: $text, TYPE: $type";
}

Open in new window

HTH, ~Ray
srikanth saladi

ASKER
Hi Ray,

I have checked the code and saw the ouptput as it displays like this

TEXT: disc 1, TYPE: disc_title
TEXT: cd, TYPE: cd_title
TEXT: disk asdf, TYPE: disc_text
TEXT: cd has ddf, TYPE: cd_text
TEXT: disc_img.png, TYPE: disc_image

But i need it in the way like disc1,diskasdf,disc_img.png all these values in an in an object or in array.From all the types (disc_title,disc_text,disc_image) we need to extract the data and store it in an object.I can access like this

for disc item using disc types
$title = $item->text;
//title = disc1
$text = $item->text;
//text = diskasdf
$Image = $item->text;
//image =disc_img.png

like wise for item cd..... all three values in an object or in array.so that i can access
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
srikanth saladi

ASKER
Hi Ray,

yaa this was helped me to format the code and display in required manner
Ray Paseur

Please see the EE grading guidelines.
http://support.experts-exchange.com/customer/portal/articles/481419

Why did you mark the grade down?  I thought a tested and working code sample was a pretty good response.  Please let us know what you expected instead!
srikanth saladi

ASKER
Hi Ray,

Thank you,your sample code was helped me to think and format they way I want the data.But it is not the complete solution what I expect
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

This is the second time you have given a marked down grade when I have given you a tested and working code set that demonstrates exactly how to solve the problem.  A reputation for giving bad grades is not something you would want to cultivate at EE.

If you want a complete solution you should hire a professional developer to create a complete solution.  EE is good for learning the principles and getting your questions answered, but you have to be realistic.  You can't expect to get your applications developed by unpaid volunteers.