XML PHP Pretty Printer


<?
/** Prettifies an XML string into a human-readable and indented work of art
 *  @param string $xml The XML as a string
 *  @param boolean $html_output True if the output should be escaped (for use in HTML)
 */
function xmlpp($xml, $html_output=false) {
    $xml_obj = new SimpleXMLElement($xml);
    $level = 4;
    $indent = 0; // current indentation level
    $pretty = array();
    
    // get an array containing each XML element
    $xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));

    // shift off opening XML tag if present
    if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
      $pretty[] = array_shift($xml);
    }

    foreach ($xml as $el) {
      if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
          // opening tag, increase indent
          $pretty[] = str_repeat(' ', $indent) . $el;
          $indent += $level;
      } else {
        if (preg_match('/^<\/.+>$/', $el)) {            
          $indent -= $level;  // closing tag, decrease indent
        }
        if ($indent < 0) {
          $indent += $level;
        }
        $pretty[] = str_repeat(' ', $indent) . $el;
      }
    }   
    $xml = implode("\n", $pretty);   
    return ($html_output) ? htmlentities($xml) : $xml;
}

echo '<pre>' . xmlpp($xml, true) . '</pre>';
?>

42 comments:

clb said...

nice code you have.

axah said...

Hey Eric,

great work. Runs out of the box :-)

Thanks

Tim Chambers said...

Excellent code - worked fine for me.

You saved me lots of time - a valuable commodity.

Regards,

Tim

Eric (Google) said...

Thanks! Glad this helped.

Eric

Unknown said...

Hey Eric, short and sweet code.
Thanks :)

Anonymous said...

Excellent, thanks a lot!

Kawika said...

This function helped me out a lot. Thanks!

thInk said...

can anyone please tell me how do i use this function?

Desiree said...

brilliant... nice and easy!

Unknown said...

Thx for the code!
Funciona perfectamente!

Patty Hayes said...

Would you be willing to put a license on this code? public-domain? GPL? bsd? I'd like to use it, but legally can't until you do!

Eric (Google) said...

At the bottom:

All code licensed under the Apache 2.0 License.

Frank de Weger said...

I might be missing something really obvious, but i think if you leave out the declaration of the SimpleXMLEelement and use

$xml = explode("\n", preg_replace('/>\s*\n<", $xml));

instead, you get the same reults...

kosovohp03 said...

healthtips
healthtips
healthtips
healthtips
healthtips




socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
check socks proxy
socks proxy
socks proxy






funnystory
funnystory
funnystory
funnystory
funnystory




funny pictures
funny pictures
funny pictures
funny pictures
funny pictures
funny pictures






Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh





cute animal
food news
internet
football news
bank news
marketing
science news
travel vietnamt
real estate vietnam
business news
music news
health news
software
seo news

kosovohp03 said...

Girl Xinh | Hot Girl | Cute Girl | Sexy Girl |Hot Girl Asian | Hot Girl Xinh
Funny Story | Funny Stories | Funny | lol
Funny Pictures | funny | lol
Girl Xinh | Hot Girl | Cute GirlSexy Girl |Hot Girl Asian | Hot Girl Xinh | lexi belle | ngoc trinh

LayZee said...

In PHP versions prior to 5.3 make sure to set encoding and other options for htmlentities.

epictoon said...

thanks for this great tip... :)
delicious


Sara said...

All the meta tags that are displayed here are wrong, i always look at those kind of thing before adding on my site maybe i can check it out for you if you get a whatsapp download

Unknown said...

reademore
lihat ini
kunjungi ini
sumber
kunjungi ini

kunjungi ini
baca selengkapnya
sumber
lihat ini

kunjungi ini
klik disini
baca selengkapnya<
lihat ini
sumber

Unknown said...

kunjungi ini
sumber
sumber
sumber
sumber
sumber
sumber
sumber
sumber
klik disini

Unknown said...

lihat penawaran kami
lihat artikel
read more
klik disini
kunjungi website
kami tawar
obat

Unknown said...

OK
Fashion hijab bisnis bersinar | Produk toko online laris | Rahasia sukses jual baju online | Bisnis paling laku dipasaran | Tips sukses usaha katering | Usaha sampingan di rumah | Usaha sampingan ibu rumah tangga | Usaha sampingan karyawan | Usaha sampingan modal kecil | Usaha sampingan tanpa modal | Harga mesin sablon digital | Harga mesin las listrik | Harga mesin bubut | Harga mesin jahit juki | Harga mesin cuci 2 tabung | Jenis jerawat | Penyebab jerawat | Menghilangkan jerawat jeruk nipis | Menghilangkan jerawat batu alami | Menghilangkan bekas jerawat cepat |

Unknown said...

nice function.
thanx

Unknown said...

Hi i love computer too. Thanks for sharring this post. Nice blog :)
Masker Roro Mendut Jerawat Asli
Masker Roro Mendut Untuk Jerawat Murah
Obat Jerawat Roro Mendut Terbaik
Lulur Jerawat Roro Mendut Gratis Ongkir !

Unknown said...

Hi, your blog has great informative and awesome posts. I am very glad to read it . IT increased my knowledge. thanks for sharing.
Obat Penghancur Lemak Darah

Unknown said...

2017 honda crv

model rumah minimalis

Tutorial Hijab

kingrani said...

Flipkart is Offering Cashback Offers Flipkart Today mobile phone Offers You will get upto 75% Discount

Flipkart is Offering Cashback Offers Flipkart Today bank Offers You will get upto 75% Discount

Flipkart is Offering Cashback Offers Flipkart deal of the day offers You will get upto 75% Discount
Flipkart is one of the biggest world wide eCommerce Site so here you will getflipkart bank cashback offersso

Flipkart is one of the biggest world wide eCommerce Site so here you will getFlipkart bank offersso

beng beng said...

its nice program, but im little migrain weight loss tips for women

T Homes Hyderabad said...



flipkart sbi card,
flipkart sbi debit card emi,
flipkart sbi card offer,
flipkart offer with sbi card,
flipkart sbi credit card offer,
flipkart sbi debit card emi offer,
sbi woohoo flipkart gift card,
flipkart sbi debit card emi eligibility check,
flipkart sbi card offer 2019,
sbi card flipkart voucher offer,
flipkart sbi debit card emi link,
flipkart sbi card offer big billion day
flipkart debit card emi sbi in hindi,
flipkart sbi debit card emi eligibility check sms,
flipkart sbi card offer terms and conditions,
flipkart coupons for sbi credit card,
sbi credit card flipkart voucher offer,
flipkart big billion day sbi card,
flipkart sbi card offer 2017,
flipkart sbi card offer today




flipkart sbi debit card emi,
flipkart emi on sbi debit card,
flipkart sbi debit card offer,
flipkart sbi debit card emi eligibility sms,
flipkart sbi debit card emi offer,
flipkart sbi debit card emi eligibility check,
flipkart debit card emi sbi sms,
flipkart sbi debit card emi link,
flipkart debit card emi sbi in hindi,
flipkart sbi debit card emi eligibility check sms,
flipkart offer for sbi debit card,
10 off on flipkart sbi debit card,
flipkart sbi debit card emi option,
flipkart offer with sbi debit card,
flipkart and sbi debit card offer




flipkart sbi emi,,
flipkart emi on sbi debit card,
flipkart sbi debit card emi eligibility,
flipkart sbi debit card emi eligibility sms,
flipkart sbi debit card emi offer,
flipkart sbi debit card emi eligibility check,
flipkart sbi emi debit card,
flipkart debit card emi sbi mobile,
flipkart sbi debit card emi link,
flipkart sbi debit card emi 2018,
flipkart debit card emi sbi in hindi,
flipkart sbi debit card emi eligibility check sms,
flipkart sbi emi offer

T Homes Hyderabad said...



flipkart hdfc,
flipkart hdfc offer,
flipkart hdfc credit card offer,
flipkart offers for hdfc credit card,
flipkart hdfc offer terms and conditions,
flipkart hdfc debit card emi,
flipkart hdfc card offers,
flipkart hdfc debit card offer,
flipkart coupons for hdfc,
flipkart hdfc offer 2018,
flipkart offer with hdfc credit card,
flipkart hdfc smart buy,
flipkart hdfc offer august 2017,
flipkart emi hdfc debit card,
flipkart hdfc debit card emi offer,
flipkart hdfc emi offer,
flipkart loan from hdfc,
flipkart hdfc promo code,
flipkart sale hdfc offer,
flipkart hdfc emi,
flipkart and hdfc offer,
flipkart hdfc terms and conditions,
flipkart hdfc no cost emi,
flipkart hdfc bank,
flipkart hdfc credit card emi,
flipkart hdfc sale,
flipkart and hdfc,
flipkart and hdfc credit card offer,
flipkart discount hdfc credit card,
flipkart freedom sale hdfc offer,
flipkart hdfc 10 instant ,
flipkart hdfc bank offer 2017,
flipkart hdfc offer 2017,
flipkart hdfc offer july 2017,
flipkart hdfc offer may 2017



flipkart hdfc offers,
flipkart hdfc offer,
flipkart offers for hdfc credit card,
flipkart offers on hdfc credit card,
flipkart hdfc offer terms and conditions,
flipkart hdfc card offers,
flipkart hdfc offer 2018,
flipkart hdfc offer august 2017,
flipkart hdfc emi offer,
flipkart offers with hdfc credit card,
flipkart hdfc offer may 2018,
flipkart hdfc instant discount,
flipkart hdfc offer today,
flipkart hdfc card offer 2017,
flipkart hdfc offer instant discount,
flipkart hdfc offer july 2017,
flipkart hdfc offer may 2017




flipkart hdfc card,
flipkart hdfc credit card offer,
flipkart hdfc debit card emi,
flipkart hdfc card offer,
flipkart hdfc debit card emi offer,
flipkart hdfc debit card emi products,
flipkart hdfc bank debit card emi,
flipkart debit card emi hdfc sms,
flipkart debit card emi option hdfc,
flipkart hdfc debit card emi eligibility,
flipkart hdfc credit card emi




flipkart hdfc debit card emi,
flipkart hdfc debit card offer,
flipkart no cost emi hdfc debit card,
flipkart discount on hdfc debit card,
flipkart hdfc debit card emi offer,
flipkart debit card emi hdfc sms,
flipkart hdfc debit card emi eligibility




flipkart hdfc emi,
flipkart hdfc debit card emi,
flipkart no cost emi hdfc debit card,
flipkart emi hdfc debit card,
flipkart hdfc debit card emi products,
flipkart hdfc debit card emi offer,
flipkart debit card emi hdfc sms,
flipkart hdfc emi offer,
flipkart hdfc debit card emi eligibility,
flipkart hdfc no cost ,
flipkart hdfc credit card emi

T Homes Hyderabad said...


flipkart emi for debit card,
flipkart debit card emi,
flipkart emi debit card sbi,
flipkart sbi debit card emi,
flipkart icici debit card emi,
flipkart emi debit card mobile,
flipkart debit card offers,
flipkart offer debit card,
flipkart debit card emi icici,
flipkart sbi debit card offer,
flipkart hdfc debit card emi
flipkart emi on debit card sbi,
flipkart axis debit card emi,
flipkart hdfc debit card offer,
flipkart icici debit card offer,
flipkart emi debit card icici,
flipkart emi debit card axis bank,
flipkart sbi debit card emi offer,
flipkart sbi debit card emi eligibility check,
flipkart refund to debit card,
flipkart debit card emi purchase,
flipkart debit card emi message,
flipkart debit card emi products list,
flipkart axis bank debit card emi,
flipkart debit card emi terms and conditions,
flipkart debit card emi bob,
flipkart debit card emi news,
flipkart debit card emi iob,
flipkart emi without debit card,
flipkart debit card loan,
flipkart debit card emi ,
flipkart pe debit card emi kaise banaye,
flipkart hdfc debit card emi products,
flipkart debit card no cost emi,
flipkart accept debit card for emi,
flipkart debit card emi banks,
flipkart debit card emi option hdfc,
flipkart hdfc debit card emi offer,
flipkart debit card emi video,
flipkart par debit card se emi,
flipkart debit card emi hdfc sms,
flipkart debit card emi kaise kare,
flipkart hdfc bank debit card emi,
flipkart emi on debit card quora,
flipkart debit card emi union bank,
flipkart debit card emi tamil,
flipkart debit card emi in hindi,
flipkart debit card emi bank of baroda,
flipkart debit card emi indian bank,
flipkart debit card emi sbi in hindi,
flipkart debit card emi hindi,
flipkart debit card emi yes bank,
flipkart sbi debit card emi eligibility check sms,
flipkart debit card payment,
flipkart debit card payment offers,
flipkart debit card emi not eligible,
flipkart debit card emi number,
flipkart debit card emi youtube,
flipkart icici bank debit card emi,
flipkart hdfc debit card emi eligibility,
flipkart debit card emi rules,
flipkart debit card emi 2018,
flipkart debit card emi indusind bank,
flipkart axis debit card offer,
flipkart axis bank debit card offer,
flipkart debit card ad,
flipkart pnb debit card offer,
flipkart debit card emi quora,
flipkart axis bank debit card

flipkart offers said...



flipkart credit,
flipkart cardless credit,
flipkart offer for credit card,
flipkart offers with credit cards,
flipkart hdfc credit card offer,
flipkart offer credit card,
flipkart credit card offers,
flipkart cardless credit registration,
flipkart credit card,
flipkart icici credit card offer,
flipkart sbi credit card offer,
flipkart credit card emi,
flipkart credit card generator,
flipkart cardless credit not showing,
flipkart 60000 credit limit,
flipkart emi credit card,
flipkart gst credit,
flipkart instant credit,
flipkart credit limit,
flipkart refund policy for credit card,
flipkart credit line,
flipkart axis bank credit card offer,
flipkart credit 60000,
flipkart refund to credit card,
flipkart without credit card emi,
flipkart credit offers,
flipkart credit account,
flipkart cardless credit offer,
flipkart sbi credit card,
flipkart cashless credit apply,
flipkart axis credit card offer,
flipkart cardless credit how to apply,
flipkart cardless credit launch date
flipkart credit card holder,
flipkart purchase without credit card,
emi in flipkart using credit card,
flipkart international credit card,
flipkart credit emi,
flipkart emi with credit card,
flipkart credit card offers today,
flipkart hsbc credit card offers,
flipkart cash on delivery credit card
flipkart no cost emi credit card,
flipkart discount credit card,
flipkart credit card emi process,
flipkart credit card emi scheme,
flipkart mobile credit card offers,
flipkart 5000 credit,
flipkart credit facility,
flipkart credit card cash back offer,
how flipkart credit card emi works,
flipkart kotak credit card offer,
flipkart credit balance,
flipkart accepted credit cards,
flipkart credit scheme,
flipkart credit line sign up,
flipkart credit card offers 2018,
flipkart cardless credit option not showing,
flipkart hdfc bank credit card offers,
flipkart credit less,
flipkart cardless credit terms and conditions,
flipkart cardless credit hindi,
flipkart credit system,
flipkart free credit,
flipkart credit card refund,
flipkart cardless credit registration link,
flipkart home credit,
flipkart cardless credit payment,
flipkart hdfc credit card emi,
flipkart cardless credit quora,
flipkart cardless credit faq,
flipkart credit card payment,
flipkart american express credit card,
flipkart cardless credit starting date,
flipkart credit rating,
flipkart credit upto 60000,
flipkart hdfc credit card cash back offer,
flipkart input tax credit,
flipkart us credit card




flipkart credit emi,
flipkart emi without credit card,
flipkart credit card emi,
flipkart emi credit card,
flipkart mobile emi without credit card,
flipkart emi mobiles without credit card,
flipkart without credit card emi,
flipkart credit card emi process,
flipkart emi with credit card,
flipkart emi option without credit card,
flipkart no cost emi on credit card,
flipkart no cost emi credit card,
flipkart cardless credit emi,
how flipkart credit card emi works

CareerTheta said...

Thanks for the awesome post by - Bihar Govt Jobs | 12th Pass Sarkari Jobs

OfficeShoppie said...

Buy office stationery online at low prices only in officeshoppie.com
OfficeShoppie is the best Office stationery supplies in Bangalore, Office stationery supplier in Bangalore, Office stationery online at low prices, Notepads | Dairies | Planners, Office cleaning products supplier in Bangalore, Housekeeping products supplier in Bangalore, online Tissues supplier in Bangalore, Hygienic Tissues for office, Office cleaning agents supplies in Bangalore, Cleaning agents supplier in Bangalore, Office snacks supplier in Bangalore, Office snacks supplies in Bangalore, Snacks for office,Office snacks delivers in Bangalore, Office pantry supplier in Bangalore, Office pantry supplies in Bangalore, Office pantry items supplies in Bangalore, Office Pantry items supplier in Bangalore

flipkart offers said...


flipkart icici,
flipkart icici debit card emi,
flipkart icici credit card offer,
flipkart offer for icici credit card,
flipkart icici offer,
flipkart icici debit card emi offer,
flipkart icici debit card offer,
flipkart debit card emi icici bank,
flipkart icici debit card emi eligibility,
flipkart icici offer 2018,
flipkart icici emi,
flipkart emi icici debit card,
flipkart coupons for icici debit card,
flipkart coupon code for icici credit card,
flipkart offers icici card,
flipkart coupons icici,
flipkart icici bank debit card emi,
icici flipkart emi offer,
flipkart icici bank,
flipkart icici discount,
flipkart icici cashback offer,
flipkart icici offer 2019,
icici bank flipkart gift voucher,,
flipkart offer with icici bank,
flipkart icici credit card,
flipkart icici bank offer,,,
flipkart icici offer april 2019,
flipkart offers for icici debit card,
flipkart icici emi.
flipkart icici debit card emi.
flipkart icici debit card emi offer.
flipkart debit card emi icici bank.
flipkart icici debit card emi eligibility.
flipkart emi icici debit card.
flipkart emi on icici debit card.
flipkart icici bank debit card emi,
icici flipkart emi offer


flipkart sbi emi,
flipkart emi on sbi debit card,
flipkart sbi debit card emi,
flipkart sbi debit card emi eligibility,
flipkart sbi debit card emi eligibility sms,
flipkart sbi debit card emi offer,
flipkart debit card emi sbi process,
flipkart sbi debit card emi eligibility check,
flipkart sbi debit card emi products,
flipkart no cost emi on sbi debit card,
flipkart emi through sbi debit card,
flipkart sbi emi debit card,
flipkart debit card emi sbi sms,
sbi flipkart pre approved emi,
flipkart sbi debit card emi link,
flipkart sbi debit card emi 2018,
flipkart emi debit card sbi video,
flipkart emi sbi credit card,,
flipkart emi on sbi credit card,
flipkart debit card emi sbi in hindi,
flipkart sbi debit card emi eligibility check sms,
flipkart sbi debit card emi option,
flipkart sbi emi offer,
flipkart sbi debit emi,
sbi emi for flipkart,

megabudshop said...

Cannabis, also known as marijuana among other names, is a psychoactive drug from the Cannabis plant used for medical or recreational purposes. The main psychoactive part of cannabis is tetrahydrocannabinol, one of the 483 known compounds in the plant, including at least 65 other cannabinoids

buy real weed online
how to buy weed online
buy legal weed online
buy recreational weed online
buy weed edibles online
can i buy weed online
buy medical weed online
buy weed online canada
buying weed online reviews
buy weed online legit
buy weed online without medical card
buy weed seeds online canada
order marijuana online
order marijuana seeds online
how to order marijuana online
order marijuana online without a medical card
can you order medical marijuana online
order marijuana online

sravani konda said...


Intresting news, Great job, Check for a while in the given below

Latest Jobs in India


HMWSSB Manager Jobs

MPPSC PSI Syllabus 2020

Cantonment Board Barrackpore Admit Card 2020

KPSC FDA SDA Syllabus

KPSC FDA SDA Recruitment 2020

Unknown said...

https://www.golden-forum.com/memberlist.php?mode=viewprofile&u=103268

Unknown said...

123.hp.com/setup

augustwalker said...

Connect Your HP Printer Using wps pin hp printer: · Once you go into all program section. · Thereafter, you need to go into printer setup and software option. Also visit - Wps Pin Hp Envy Printer

Printer Repair City said...

Amazing, learn how to resolve Canon Printer Error u052 and solve canon printer error 6a81 Also canon printer error 5200 and this is good Canon Printer Error Code 1007 find out canon printer error e04 as well as canon printer error code p22

NewlyAussie said...


I tried the code while fixing my project. It worked.

Thanks for whatever I learned.
Coding Bootcamp Sydney
That's awesome