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
srikanth saladiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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 PaseurCommented:
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 saladiAuthor Commented:
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
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Ray PaseurCommented:
If you're new to PHP, this article has some good "getting started" suggestions and links to carefully vetted learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

See if this gives you some ideas about how to format the information.  The risk here would be that you might have a name collision on content and that would cause overwriting data that was stored earlier.

<?php // RAY_temp_srikanth_saladi.php
error_reporting(E_ALL);
echo '<pre>';

// 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);

// 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";
}
echo PHP_EOL;

// SHOW HOW TO USE THE type AS A KEY AND THE text AS A VALUE IN AN ARRAY
$out = array();
foreach ($obj->textitem as $item)
{
    $type = trim($item->type);
    $out[$type] = trim($item->text);
}
var_dump($out);
echo PHP_EOL;

// SHOW HOW TO USE THE type AS A PROPERTY NAME AND THE text AS A PROPERTY VALUE IN AN OBJECT
$out = new StdClass;
foreach ($obj->textitem as $item)
{
    $type = trim($item->type);
    $out->$type = trim($item->text);
}
var_dump($out);
echo PHP_EOL;

// SHOW HOW TO ECHO INFORMATION FROM THE OBJECT
$property = 'disc_image';
$value    = (string)$out->$property;
echo PHP_EOL . "$property CONTAINS $value";

Open in new window

Best regards, ~Ray

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikanth saladiAuthor Commented:
Hi Ray,

yaa this was helped me to format the code and display in required manner
Ray PaseurCommented:
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 saladiAuthor Commented:
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
Ray PaseurCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.