?
Solved

data fetch from xml

Posted on 2011-03-09
7
Medium Priority
?
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Assisted Solution

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

Accepted Solution

by:
Beverley Portlock earned 1200 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 400 total points
ID: 35081467
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 1200 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

Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

Question has a verified solution.

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

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 how to create an extensible mechanism for linked drop downs.
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 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 …
Suggested Courses

762 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