Solved

data fetch from xml

Posted on 2011-03-09
7
503 Views
Last Modified: 2012-05-11
Hi,
following is the xml format which has to be rendered.
I want to know how to access the specific node and how to convert it into array.
Specially node name
' <application><clients>........ </clients></application>'.

<?xml version="1.0" encoding="UTF-8"?>
<clientApplication>
  <client_application_id>2</client_application_id>
  <client_id>1</client_id>
  <application_name>Intergamma</application_name>
  <application_desc>http://www.abc.com/chain=intergamma</application_desc>
  <application_url>http://www.abc.com/</application_url>
  <access_key>gamma</access_key>
  <ip_low>127.0.0.0</ip_low>
  <ip_high>127.0.0.1</ip_high>
  <default_i18n_code></default_i18n_code>
  <is_active>Y</is_active>
  <created></created>
  <modified></modified>
  <createdby></createdby>
  <modifiedby></modifiedby>
  <clients><user_id>1</user_id><role_id>1</role_id><prefix>mr</prefix><first_name>xyz</first_name><middle_name>r</middle_name><last_name>nag</last_name><email>abc@ya.com</email><username>test</username><is_active>Y</is_active><lastlogin_IP></lastlogin_IP><lastlogin></lastlogin><company_name>Intergamma</company_name><contact_person_name>jj</contact_person_name><address1>amsterdam</address1><address2>netherland</address2><zipcode></zipcode><phone2></phone2><mobile></mobile><phone1></phone1><email1></email1><email2></email2><logo></logo><website></website></clients>
  <modules><module_permission_id>1</module_permission_id><module_id>1</module_id><permission_code>VIEW</permission_code><created></created><modified></modified><module_title>TESTER</module_title><module_desc></module_desc><module_link></module_link><module_icon></module_icon><module_help></module_help><is_active>Y</is_active><role_id>1</role_id><role_title>View</role_title><role_description></role_description><role_is_active>Y</role_is_active><client_application_id>1</client_application_id></modules>
  <application><clients><client_application_id>2</client_application_id><client_id>1</client_id><application_name>Intergamma</application_name><application_desc>http://www.abc.com/chain=intergamma</application_desc><application_url>http://www.abc.com/</application_url><access_key>gamma</access_key><ip_low>127.0.0.0</ip_low><ip_high>127.0.0.1</ip_high><default_i18n_code></default_i18n_code><is_active>Y</is_active><created></created><modified></modified><createdby></createdby><modifiedby></modifiedby></clients><clients><client_application_id>1</client_application_id><client_id>1</client_id><application_name>Gamma</application_name><application_desc>test</application_desc><application_url>http://www.abc.com/</application_url><access_key>intergamma</access_key><ip_low>127.0.0.0</ip_low><ip_high>127.0.0.1</ip_high><default_i18n_code>en</default_i18n_code><is_active>Y</is_active><created>0000-00-00 00:00:00</created><modified>0000-00-00 00:00:00</modified><createdby>1</createdby><modifiedby>1</modifiedby></clients><clients><client_application_id>3</client_application_id><client_id>1</client_id><application_name>Karwei</application_name><application_desc></application_desc><application_url>http://www.abc.com/</application_url><access_key>karwei</access_key><ip_low>127.0.0.0</ip_low><ip_high>127.0.0.1</ip_high><default_i18n_code></default_i18n_code><is_active>Y</is_active><created></created><modified></modified><createdby></createdby><modifiedby></modifiedby></clients></application>
</clientApplication>

Open in new window

0
Comment
Question by:Insoftservice
7 Comments
 
LVL 8

Assisted Solution

by:vsudip
vsudip earned 100 total points
ID: 35080823
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 300 total points
ID: 35081438
Use simplexml http://www.php.net/simplexml and proceed along these lines

<?php

$data = '<?xml version="1.0" encoding="UTF-8"?>
<clientApplication>
  <client_application_id>2</client_application_id>
  <client_id>1</client_id>
  <application_name>Intergamma</application_name>
  <application_desc>http://www.abc.com/chain=intergamma</application_desc>
  <application_url>http://www.abc.com/</application_url>
  <access_key>gamma</access_key>
  <ip_low>127.0.0.0</ip_low>
  <ip_high>127.0.0.1</ip_high>
  <default_i18n_code></default_i18n_code>
  <is_active>Y</is_active>
  <created></created>
  <modified></modified>
  <createdby></createdby>
  <modifiedby></modifiedby>
  <clients><user_id>1</user_id><role_id>1</role_id><prefix>mr</prefix><first_name>xyz</first_name><middle_name>r</middle_name><last_name>nag</last_name><email>abc@ya.com</email><username>test</username><is_active>Y</is_active><lastlogin_IP></lastlogin_IP><lastlogin></lastlogin><company_name>Intergamma</company_name><contact_person_name>jj</contact_person_name><address1>amsterdam</address1><address2>netherland</address2><zipcode></zipcode><phone2></phone2><mobile></mobile><phone1></phone1><email1></email1><email2></email2><logo></logo><website></website></clients>
  <modules><module_permission_id>1</module_permission_id><module_id>1</module_id><permission_code>VIEW</permission_code><created></created><modified></modified><module_title>TESTER</module_title><module_desc></module_desc><module_link></module_link><module_icon></module_icon><module_help></module_help><is_active>Y</is_active><role_id>1</role_id><role_title>View</role_title><role_description></role_description><role_is_active>Y</role_is_active><client_application_id>1</client_application_id></modules>
  <application><clients><client_application_id>2</client_application_id><client_id>1</client_id><application_name>Intergamma</application_name><application_desc>http://www.abc.com/chain=intergamma</application_desc><application_url>http://www.abc.com/</application_url><access_key>gamma</access_key><ip_low>127.0.0.0</ip_low><ip_high>127.0.0.1</ip_high><default_i18n_code></default_i18n_code><is_active>Y</is_active><created></created><modified></modified><createdby></createdby><modifiedby></modifiedby></clients><clients><client_application_id>1</client_application_id><client_id>1</client_id><application_name>Gamma</application_name><application_desc>test</application_desc><application_url>http://www.abc.com/</application_url><access_key>intergamma</access_key><ip_low>127.0.0.0</ip_low><ip_high>127.0.0.1</ip_high><default_i18n_code>en</default_i18n_code><is_active>Y</is_active><created>0000-00-00 00:00:00</created><modified>0000-00-00 00:00:00</modified><createdby>1</createdby><modifiedby>1</modifiedby></clients><clients><client_application_id>3</client_application_id><client_id>1</client_id><application_name>Karwei</application_name><application_desc></application_desc><application_url>http://www.abc.com/</application_url><access_key>karwei</access_key><ip_low>127.0.0.0</ip_low><ip_high>127.0.0.1</ip_high><default_i18n_code></default_i18n_code><is_active>Y</is_active><created></created><modified></modified><createdby></createdby><modifiedby></modifiedby></clients></application>
</clientApplication>
';

$xml = simplexml_load_string( $data );



foreach( $xml->application->clients as $aClient ) {

     $appId = (int) $aClient->client_application_id;
     $id = (int) $aClient->client_id;
     $appName = (string) $aClient->application_name;

     echo "$appId  $id   $appName<br/>";
}

Open in new window

0
 
LVL 5

Assisted Solution

by:SimonDard
SimonDard earned 100 total points
ID: 35081467
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Author Comment

by:Insoftservice
ID: 35081535
HI,

Please check my dummy code.
But its not working please let me know where i am wrong
$doc = new DOMDocument();
$doc->load( $xml );

//$books1 = $doc->getElementsByTagName( "clientApplication" );


$books = $books1->getElementsByTagName( "application" );
   foreach( $books as $book )
    {
	$authors = $book->getElementsByTagName( "clients" );
	$author = $authors->item(0)->nodeValue;

	$publishers = $book->getElementsByTagName( "client_application_id" );
	$publisher = $publishers->item(0)->nodeValue;

	$titles = $book->getElementsByTagName( "client_id" );
	$title = $titles->item(0)->nodeValue;

	echo "$title - $author - $publisher\n==";
  }

Open in new window

0
 
LVL 15

Author Comment

by:Insoftservice
ID: 35081630
Hi,
thx @bportlock

and thx to all even i will try other method too.

it was perfect anwer what i wanted.
But actually i would not have actual name of the xml tag similar to application->clients.
Actually its converted to xml from numerative array and later it has to be again converted to array.
So, how to get tag name which is been developed from such type of array.
' <application><clients>........ </clients></application>'. was the actual example of it.
 
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 300 total points
ID: 35081761
You could combine my script with what SimonDard suggested


foreach( $xml->children() as $name => $data ) {
     echo "$name $data<br/>";
}

This will print out values and the names of the XML elements, but the whole point of an XML document is that they have a defined structure that you can use to navigate. If someone is making up these XML files on a willy-nilly basis and expecting you to process anything they send, then you're in for a bad time.
0
 
LVL 15

Author Closing Comment

by:Insoftservice
ID: 35106001
Thx to all.
Happy holi in advance.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now