I needed this for work/personal use. Sometimes you'll have a XML string generated as one long string and no line breaks...nusoap in the case of today/work, but there are any other number of possible things that will generate these. Anyways, this simply takes a long XML string and returns an indented/line-breaked version of the string for display/readability.
function xmlIndent($str){
$ret = "";
$indent = 0;
$indentInc = 3;
$noIndent = false;
while(($l = strpos($str,"<",$i))!==false){
if($l!=$r && $indent>0){ $ret .= "\n" . str_repeat(" ",$indent) . substr($str,$r,($l-$r)); }
$i = $l+1;
$r = strpos($str,">",$i)+1;
$t = substr($str,$l,($r-$l));
if(strpos($t,"/")==1){
$indent -= $indentInc;
$noIndent = true;
}
else if(($r-$l-strpos($t,"/"))==2 || substr($t,0,2)==" if($indent<0){ $indent = 0; }
if($ret){ $ret .= "\n"; }
$ret .= str_repeat(" ",$indent);
$ret .= $t;
if(!$noIndent){ $indent += $indentInc; }
$noIndent = false;
}
$ret .= "\n";
return($ret);
}
?>_
(...this was only tested for what i needed at work, could POSSIBLY need additions)
XML (eXtensible Markup Language) adalah format data untuk pertukaran dokumen terstruktur di Web. Ini adalah standar yang ditentukan oleh The World Wide Web Consortium (W3C). Informasi tentang XML dan teknologi terkait dapat ditemukan di » http. // www. w3. org/XML/
Ekstensi PHP ini mengimplementasikan dukungan untuk expat James Clark di PHP. Toolkit ini memungkinkan Anda mengurai, tetapi tidak memvalidasi, dokumen XML. Ini mendukung tiga pengkodean karakter sumber yang juga disediakan oleh PHP. US-ASCII, ISO-8859-1 dan UTF-8. UTF-16_ tidak didukung
Ekstensi ini memungkinkan Anda membuat pengurai XML dan kemudian menentukan penangan untuk peristiwa XML yang berbeda. Setiap parser XML juga memiliki beberapa parameter yang dapat Anda sesuaikan
There is a common "trick" often proposed to convert a SimpleXML object to an array, by running it through json_encode() and then json_decode(). I'd like to explain why this is a bad idea.
Most simply, because the whole point of SimpleXML is to be easier to use and more powerful than a plain array. For instance, you can write bar->baz['bing'] ?> and it means the same thing as bar[0]->baz[0]['bing'] ?>, regardless of how many bar or baz elements there are in the XML; and if you write bar[0]->baz[0] ?> you get all the string content of that node - including CDATA sections - regardless of whether it also has child elements or attributes. You also have access to namespace information, the ability to make simple edits to the XML, and even the ability to "import" into a DOM object, for much more powerful manipulation. All of this is lost by turning the object into an array rather than reading understanding the examples on this page._
Additionally, because it is not designed for this purpose, the conversion to JSON and back will actually lose information in some situations. For instance, any elements or attributes in a namespace will simply be discarded, and any text content will be discarded if an element also has children or attributes. Sometimes, this won't matter, but if you get in the habit of converting everything to arrays, it's going to sting you eventually._
Of course, you could write a smarter conversion, which didn't have these limitations, but at that point, you are getting no value out of SimpleXML at all, and should just use the lower level XML Parser functions, or the XMLReader class, to create your structure. You still won't have the extra convenience functionality of SimpleXML, but that's your loss.
XML adalah bahasa mark-up untuk berbagi data di seluruh web, XML adalah untuk dapat dibaca oleh manusia dan dapat dibaca oleh mesin. Contoh xml yang dapat dibagikan adalah Umpan RSS. Parser XML berguna untuk membaca dan memperbarui data dengan menggunakan browser web
Jenis XML
Ekstensi Parse XML
Ekstensi penguraian XML berfungsi berdasarkan libxml. Parser xml berikut tersedia di inti php
- Pengurai XML sederhana
- LAKUKAN parser XML
- pengurai XML
- Pembaca XML
Pengurai XML sederhana
Parser XML Sederhana juga disebut parser XML berbasis pohon dan akan mem-parsing file XML sederhana. Parse XML sederhana akan memanggil metode simplexml_load_file() untuk mendapatkan akses ke xml dari jalur tertentu
pengurai DOM
DOM Parser juga disebut sebagai pengurai simpul kompleks, yang digunakan untuk mem-parsing file XML yang sangat kompleks. Ini digunakan sebagai antarmuka untuk memodifikasi file XML. Parser DOM telah dikodekan dengan pengkodean karakter UTF-8
XML mengurai
Penguraian XML didasarkan pada penguraian SAX. Lebih cepat semua parser di atas. Ini akan membuat file XML dan mem-parsing XML. Parser XML telah dikodekan oleh pengkodean karakter ISO-8859-1, US-ASCII dan UTF-8
Pembaca XML
Penguraian XML Reader juga disebut sebagai Pull XML parse. Ini digunakan untuk membaca file XML dengan cara yang lebih cepat. Ini bekerja dengan dokumen XML kompleks tinggi dengan Validasi XML