» » Conversion between PHP array and XML

 

Conversion between PHP array and XML

Author: bamboo06 on 9-04-2017, 00:59, views: 2287

88
In the development, we often encounter the conversion between the array and XML, especially when dealing with interface development often used, such as the other side of the client POST a XML format data to the server, the server program is responsible for receiving Analysis, there is a need to data table data in XML format to provide third-party applications.
Conversion between PHP array and XML

In this article we will briefly explain how to use PHP to deal with the conversion between the array and XML.

PHP converts an array to XML
PHP can convert the array into xml format, the simple way is to traverse the array, and then the array of key / value into xml node, and then directly echo output, such as:
function arrayToXml($arr){ 
    $xml = "<root>"; 
    foreach ($arr as $key=>$val){ 
        if(is_array($val)){ 
            $xml.="<".$key.">".arrayToXml($val)."</".$key.">"; 
        }else{ 
            $xml.="<".$key.">".$val."</".$key.">"; 
        } 
    } 
    $xml.="</root>"; 
    return $xml; 
} 

I tested the next, the most simple, fast and fast, support for the array.
Another way is to use DOMDocument to generate xml structure:
function arrayToXml($arr,$dom=0,$item=0){ 
    if (!$dom){ 
        $dom = new DOMDocument("1.0"); 
    } 
    if(!$item){ 
        $item = $dom->createElement("root");  
        $dom->appendChild($item); 
    } 
    foreach ($arr as $key=>$val){ 
        $itemx = $dom->createElement(is_string($key)?$key:"item"); 
        $item->appendChild($itemx); 
        if (!is_array($val)){ 
            $text = $dom->createTextNode($val); 
            $itemx->appendChild($text); 
             
        }else { 
            arrayToXml($val,$dom,$itemx); 
        } 
    } 
    return $dom->saveXML(); 
} 

It can also convert an array to xml and support multidimensional arrays.

PHP converts XML into an array
Do interface development often encounter someone else to submit to you is the xml format data, the common WeChat interface, Alipay interface, etc., their interface, such as sending messages are xml format, then we first find a way to get this Xml data, and then convert it into an array.
Suppose we get an XML like this:
<root> 
<user>goocode</user> 
<pvs>12345</pvs> 
<ips> 
<google_ip>1200</google_ip> 
<bing_ip>1829</bing_ip> 
</ips> 
<date>2017-04-01</date> 
</root> 

Through simplexml_load_string () parsing read xml data, and then first converted to json format, and then converted into an array.
function xmlToArray($xml){     
    //Do not reference external xml entities
    libxml_disable_entity_loader(true); 
    $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 
    $val = json_decode(json_encode($xmlstring),true);   
    return $val; 
} 

Call the function to get the array, we can carry out a variety of data processing.

Category: PHP Scripts

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
18 November 2017 09:41

bennieben

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
this is a great post. i had this issue when i was working at BritishEssays.net, and now I get what was wrong. thanks a lot! stay strong my dude!

<
  • 0 Comments
  • 0 Articles
21 November 2017 23:13

BerthaWob

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
need a loan now
kwikcash
<a href="https://loanstrast.com/">taking out a loan</a>
loans with poor credit
bank personal loans
<a href="https://loansfast.us.com/">social loan</a>
sos loans

loans online with monthly payments
unsecured loans
<a href="https://loanstrast.com/">apply for loan with bad credit</a>
payday loans direct lenders bad credit
100 loans
<a href="https://loansord.us.com/">quickcash</a>
direct lender loans

<
  • 0 Comments
  • 0 Articles
23 November 2017 06:30

VictorVobby

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
best pharmacy to buy cialis
<a href="http://cialismbvi.com/#">cialis prices</a>
can cialis pills be split
cialis online
cialis buy online uk

<
  • 0 Comments
  • 0 Articles
23 November 2017 10:21

Nelsonraift

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
cialis for sale manila
<a href="http://cialismbvi.com/#">online cialis</a>
buy brand name cialis online
buy cialis
buy cialis generic online cheap

<
  • 0 Comments
  • 0 Articles
23 November 2017 18:56

JamesSip

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
cheap cialis rx
<a href="http://cialismbvi.com/#">buy cialis online</a>
cialis 5 mg cheap
buy cialis
buy cialis legally online

<
  • 0 Comments
  • 0 Articles
24 November 2017 09:46

FloydRusty

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
viagra buy online forum
<a href="http://viagramndet.com/#">viagra cost</a>
is buying online viagra safe
cost of viagra
generic names of viagra

<
  • 0 Comments
  • 0 Articles
24 November 2017 18:34

LouisTig

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
can viagra pill cut
<a href="http://viagramndet.com/#">viagra cost</a>
where to buy viagra over the counter in the uk
viagra coupons
viagra generico in farmacia senza ricetta

<
  • 0 Comments
  • 0 Articles
25 November 2017 20:00

Mathewscuro

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
viagra 40 mg
<a href="http://viagramhbfe.com/#">viagra coupons 75 off</a>
buy viagra uk tesco
viagra coupons 75 off
singapore viagra price

safe buy viagra online forum
<a href="http://viagramhbfe.com/#">viagra prices</a>
current price of viagra
viagra price
viagra 150 mg safe

<
  • 0 Comments
  • 0 Articles
25 November 2017 23:39

SarahMeern

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
direct deposit advance
get a personal loan
<a href="https://loanstrast.com/">free loan</a>
cash loan bad credit

<
  • 0 Comments
  • 0 Articles
26 November 2017 11:13

JarrodWal

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
cutting cialis pills half
<a href="http://cialisopghe.com/#">cialis coupons</a>
cheap cialis prices uk
coupon for cialis
discount canadian pharmacy cialis

<
  • 0 Comments
  • 0 Articles
27 November 2017 04:40

YvonneFer

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
payday loans bad credit
loans online
<a href="https://loanstrast.com/">payday loans</a>
payday loans

<
  • 0 Comments
  • 0 Articles
27 November 2017 22:29

Stevenedisa

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
buy genuine levitra
<a href="http://levitraklnbi.com/#">levitra 20mg</a>
buy generic viagra cialis levitra
viagra vs cialis vs levitra
purchase cheap levitra

how to order levitra online
<a href="http://levitraklnbi.com/#">viagra vs cialis vs levitra</a>
buy levitra online in canada
9 levitra at walmart
discount levitra online

<
  • 0 Comments
  • 0 Articles
28 November 2017 08:59

Douglasdiuse

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
cheapest canadian pharmacy cialis
<a href="http://cialisfbvne.com/#">cialis without a prescription</a>
cialis china cheap
cialis without prescription
buy cialis today

<
  • 0 Comments
  • 0 Articles
28 November 2017 17:10

EricaSeemi

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
payday loans no credit check
online loans
<a href="https://loanstrast.com/">payday loans</a>
loans online

<
  • 0 Comments
  • 0 Articles
29 November 2017 09:49

DennistearI

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
can you get viagra online
<a href="http://viagramnkjm.com/#">viagra without prescription</a>
generico do viagra e bom
viagra without prescription
viagra 200 mg dose

<
  • 0 Comments
  • 0 Articles
29 November 2017 21:22

Brandonnem

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
are generic viagra real
<a href="http://viagramnkjm.com/#">viagra without a doctor’s prescription</a>
acquisto viagra generico line
viagra without a doctor’s prescription
erfahrungen viagra online kaufen

<
  • 0 Comments
  • 0 Articles
30 November 2017 03:48

Susannatierb

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
payday loan online
payday loans online
<a href="https://smajloans.com/">cash advance</a>
payday loan online
payday loans no credit check
<a href="https://loanstrast.com/">online loans</a>
cash loans
payday loan online
<a href="https://loansfast.us.com/">pay day loans</a>
payday loans online

<
  • 0 Comments
  • 0 Articles
30 November 2017 09:56

TerryVarne

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
arizone on line gambling
<a href="http://online-casino.party/#">online casinos</a>
internet gambling debit card
online casino
best online casino slot machines

<
  • 0 Comments
  • 0 Articles
30 November 2017 22:30

Michaellob

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
real online casino real money
<a href="http://online-casino.party/#">casino</a>
best online casino gaming sites
casino online
playing roulette

<
  • 0 Comments
  • 0 Articles
1 December 2017 10:57

GeorgeLip

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
internet slots real money
<a href="http://online-casino.party/#">casino online</a>
online casino payout ratio
online casino
online casinos for mac in australia

<
  • 0 Comments
  • 0 Articles
1 December 2017 19:31

CraigPsype

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
online poker casino games
<a href="http://online-casino.party/#">casino</a>
the best online blackjack with money
casino online
online casinos with sign up bonus

<
  • 0 Comments
  • 0 Articles
2 December 2017 09:46

Leighwrili

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
online casino games play casino games
<a href="http://online-casino.party/#">online casino</a>
best casino gambling online vegas
casino
online casino u s players

<
  • 0 Comments
  • 0 Articles
2 December 2017 17:27

Jasonbeark

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
mobile casino for real money
<a href="http://online-casino.party/#">online casinos</a>
trusted australian online casino
online casinos
casino on line las vegas

<
  • 0 Comments
  • 0 Articles
3 December 2017 07:47

Keithler

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
<a href="https://www.carbatteryprice.loan/kinetik-car-audio-battery/titanium-3
34-car-van-battery-91ah.php">Titanium 334 Car Van Battery 91Ah</a>
Seat Ibiza St Mk5 2010-2016 Bosch S3 Battery 70Ah Electrical System Replace Part
<a href="https://www.newcarbattery.download/car-battery-cables/saab-9-5-ys3e-y
s3g-1997-2012-bosch-s3-battery-70ah-electric-system-replace-part.php">Sa
ab 9-5 Ys3E Ys3G 1997-2012 Bosch S3 Battery 70Ah Electric System Replace Part</a>
Vauxhall Antara J26 H26 2010-2016 Vetech Battery 60Ah Electrical Replace Part
<a href="https://www.carbatterybuy.bid/electric-car-battery-pack/alfa-romeo-14
7-937-2001-2010-bosch-s3-battery-77ah-electric-system-replace-part.php">
Alfa Romeo 147 937 2001-2010 Bosch S3 Battery 77Ah Electric System Replace Part</a>
Type 110 Titanium Car Battery 12V 640CCA - fits many Audi BMW Ford Merc Porsch
<a href="https://www.newcarbattery.download/car-battery-acid-on-skin/4-years-w
ty-sealed-bosch-car-battery-12v-60ah-type-027-540cca-oem-replacement.php"&g
t;4 Years Wty Sealed Bosch Car Battery 12V 60Ah Type 027 540CCA OEM Replacement</a>
Titanium 075 Car Battery 12V 62Ah - Fast & Free Delivery

<
  • 0 Comments
  • 0 Articles
3 December 2017 09:48

GloriaShola

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
pay day loan
online loans
<a href="https://smajloans.com/">cash advance loans</a>
payday loans
cash advance
<a href="https://loanstrast.com/">cash loans</a>
payday advance
cash loans
<a href="https://loansfast.us.com/">cash advance</a>
pay day loan

<
  • 0 Comments
  • 0 Articles
3 December 2017 10:33

BrucecoT

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
jugar blackjack online casino
<a href="http://online-casino.party/#">online casinos</a>
canadian online casinos mac
casino real money
online casinos that accept american express

<
  • 0 Comments
  • 0 Articles
5 December 2017 13:56

Louisewandy

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
cash loans
cash advance loans
<a href="https://smajloans.com/">pay day loan</a>
online loans
pay day loans
<a href="https://loanstrast.com/">cash advance loans</a>
cash loans
online loans
<a href="https://loansfast.us.com/">pay day loan</a>
pay day loans

<
  • 0 Comments
  • 0 Articles
6 December 2017 16:10

Chrisapose

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
buy cialis edmonton
<a href="http://cialisviymw.com/#">cialis on line no pres</a>
how to buy viagra or cialis
cialis without doctor
cheapest cialis internet

<
  • 0 Comments
  • 0 Articles
7 December 2017 18:13

Marthaglild

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
casino games
best us casinos online
<a href="http://real777money.com/">free online casino games</a>
real money casino online usa

<
  • 0 Comments
  • 0 Articles
10 December 2017 13:20

GingerAmoms

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
free casino games
free slot games
<a href="http://real777money.com/">casino online</a>
online casino

Previous Next

Add comments

Name:*
E-Mail:
Comment:
Bold Italic Underline Strike | Align left Center Align right | Insert smilies Insert link URLInsert protected URL Select color | Add Hidden Text Insert Quote Convert selected text from selection to Cyrillic (Russian) alphabet Insert spoiler
Security Code: *