Solved

data fetch from xml

Posted on 2011-03-09
7
506 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 36
Echo vs ?><?php  html code 4 45
PHP script redirecting to URL not working on AWS EC2 after Elastic IP 12 64
phpmyadmin 3 33
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…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

20 Experts available now in Live!

Get 1:1 Help Now