Discussion: mutual conversion between array2xml, xml2array, and xml and array

Source: Internet
Author: User

Php often encounters this situation when working as a backend server. It needs to parse xml files from the foreground and return the data in xml format. In this case, the conversion of arrays associated with xml and php is very frequent. For example, this method is often used for interaction between flex and other client programs and servers. The following are two methods I have summarized, which greatly simplifies the workload of conversion between xml and arrays.

Copy codeThe Code is as follows :/**
*
* Convert a simple array to a simple xml
* @ Param string $ array of data to be converted
* @ Param string $ tag the tag to use
* @ Example
* $ Arr = array (
'Rtxaccount' => 'Aaron ', 'ipadd' => '2017. 168.0.12 ',
'Conferencelist' => array ('conference '=>
Array (
Array ('ferenceid' => 1212, 'ferencetitle' => 'quanshi 444 ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 454, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 6767, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 232323, 'ferencetitle' => 'quanshi uuu ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 8989, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 1234343212, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'HTTP: // www.jb51.net ')
)
)
);
Convert:
<RtxAccount> aaron </rtxAccount>
<IpAddr> 192.168.0.12 </ipAddr>
<ConferenceList>
<Conference>
<ConferenceId> 1212 </conferenceId>
<ConferenceTitle> quanshi 444 </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 454 </conferenceId>
<ConferenceTitle> quanshi meetting </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 6767 </conferenceId>
<ConferenceTitle> quanshi meetting </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 232323 </conferenceId>
<ConferenceTitle> quanshi uuu </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 8989 </conferenceId>
<ConferenceTitle> quanshi meetting </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 1234343212 </conferenceId>
<ConferenceTitle> quanshi meetting </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
</ConferenceList>
*/
Function array2xml ($ data, $ tag = '')
{
$ Xml = '';

Foreach ($ data as $ key => $ value)
{
If (is_numeric ($ key ))
{
If (is_array ($ value ))
{
$ Xml. = "<$ tag> ";
$ Xml. = array2xml ($ value );
$ Xml. = "</$ tag> ";
}
Else
{
$ Xml. = "<$ tag> $ value </$ tag> ";
}
}
Else
{
If (is_array ($ value ))
{
$ Keys = array_keys ($ value );
If (is_numeric ($ keys [0])
{
$ Xml. = array2xml ($ value, $ key );
}
Else
{
$ Xml. = "<$ key> ";
$ Xml. = array2xml ($ value );
$ Xml. = "</$ key> ";
}

}
Else
{
$ Xml. = "<$ key> $ value </$ key> ";
}
}
}
Return $ xml;
}
}

Xml2arrayCopy codeThe Code is as follows :/**
*
* Convert simple xml into an associated array
* @ Param string $ xmlString xml string
* @ Example
* <? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<RTXConferenceReqDTO>
<ConferenceTitle> IT exchange meeting </conferenceTitle>
<StartTime> 12:00:00 </startTime>
<RtxAccount> andy1111111 </rtxAccount>
<IpAddr> 192.168.1.56 </ipAddr>
<Duration> 120 </duration>
<ConferenceType> 1 </conferenceType>
<Invitees>
<Invitee>
<RtxAccount> RTX account of invitee 1 </rtxAccount>
<Tel> Number of the invitee 1 </tel>
</Invitee>
<Invitee>
<RtxAccount> RTX account of invitee 2 </rtxAccount>
<Tel> invitee 2 phone number </tel>
</Invitee>
</Invitees>
</RTXConferenceReqDTO>
Converted joined array:
Array
(
[ConferenceTitle] => IT Exchange Conference
[StartTime] => 12:00:00
[RtxAccount] => andy1111111
[IpAddr] => 192.168.1.56
[Duration] = & gt; 120
[ConferenceType] => 1
[Invitees] => Array
(
[Invitee] => Array
(
[0] => Array
(
[RtxAccount] => RTX account of invitee 1
[Tel] => Number of invitee 1
)
[1] => Array
(
[RtxAccount] => RTX account of invitee 2
[Tel] => invitee 2 phone number
)
)
)
)
*/
Function xml2array ($ xmlString = '')
{
$ TargetArray = array ();
$ XmlObject = simplexml_load_string ($ xmlString );
$ MixArray = (array) $ xmlObject;
Foreach ($ mixArray as $ key => $ value)
{
If (is_string ($ value ))
{
$ TargetArray [$ key] = $ value;
}
If (is_object ($ value ))
{
$ TargetArray [$ key] = xml2array ($ value-> asXML ());
}
If (is_array ($ value ))
{
Foreach ($ value as $ zkey => $ zvalue)
{
If (is_numeric ($ zkey ))
{
$ TargetArray [$ key] [] = xml2array ($ zvalue-> asXML ());
}
If (is_string ($ zkey ))
{
$ TargetArray [$ key] [$ zkey] = xml2array ($ zvalue-> asXML ());
}
}
}
}
Return $ targetArray;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.