Solved

data fetch from xml

Posted on 2011-03-09
7
516 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 count occurrences of each item in an array.

739 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