Solved

parsing xml having similar tags but different values

Posted on 2014-01-03
8
311 Views
Last Modified: 2014-02-27
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
0
Comment
Question by:srikanth_saladi
  • 5
  • 3
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39754050
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39754081
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
0
 

Author Comment

by:srikanth_saladi
ID: 39754158
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
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39754373
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Closing Comment

by:srikanth_saladi
ID: 39758983
Hi Ray,

yaa this was helped me to format the code and display in required manner
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39759105
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!
0
 

Author Comment

by:srikanth_saladi
ID: 39891762
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39891841
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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now