XML Wrangler เป็นไลบรารี PHP แบบง่าย ๆ ที่ออกแบบมาเพื่อให้การอ่านและการเขียน XML ง่าย XML Wrangler ถูกสร้างขึ้นโดยคำนึงถึงประสบการณ์ของนักพัฒนาซอฟต์แวร์ - คุณสามารถอ่านไฟล์ XML ทุกประเภทได้แม้จะมีเนมสเปซที่ซับซ้อนและแม้แต่ไฟล์ XML ขนาดใหญ่ นอกจากนี้ยังจะโยนข้อยกเว้นหาก XML ไม่ถูกต้อง!
XML Wrangler ติดตั้งผ่านนักแต่งเพลง
composer require saloonphp/xml-wrangler
ต้องใช้ PHP 8.1+
การอ่าน XML สามารถทำได้โดยผ่านสตริง XML หรือไฟล์ไปยังเครื่องอ่าน XML และใช้หนึ่งในหลายวิธีในการค้นหาและค้นหาองค์ประกอบหรือค่าเฉพาะ นอกจากนี้คุณยังสามารถแปลงองค์ประกอบทุกอย่างให้เป็นอาร์เรย์ที่สามารถเคลื่อนที่ได้อย่างง่ายดายหากคุณต้องการ หากคุณต้องการเข้าถึงแอตทริบิวต์บนองค์ประกอบคุณสามารถใช้ Element
DTO ซึ่งเป็นคลาสที่ง่ายในการเข้าถึงเนื้อหาและแอตทริบิวต์ XML Wrangler ให้วิธีการวนซ้ำผ่านองค์ประกอบหลายอย่างในขณะที่เก็บองค์ประกอบหนึ่งในหน่วยความจำในแต่ละครั้ง
< breakfast_menu >
< food soldOut = " false " bestSeller = " true " >
< name >Belgian Waffles</ name >
< price >$5.95</ price >
< description >Two of our famous Belgian Waffles with plenty of real maple syrup</ description >
< calories >650</ calories >
</ food >
< food soldOut = " false " bestSeller = " false " >
< name >Strawberry Belgian Waffles</ name >
< price >$7.95</ price >
< description >Light Belgian waffles covered with strawberries and whipped cream</ description >
< calories >900</ calories >
</ food >
< food soldOut = " false " bestSeller = " true " >
< name >Berry-Berry Belgian Waffles</ name >
< price >$8.95</ price >
< description >Light Belgian waffles covered with an assortment of fresh berries and whipped cream</ description >
< calories >900</ calories >
</ food >
</ breakfast_menu >
<?php
use Saloon XmlWrangler XmlReader ;
$ reader = XmlReader :: fromString ( $ xml );
// Retrieve all values as one simple array
$ reader -> values (); // ['breakfast_menu' => [['name' => '...'], ['name' => '...'], ['name' => '...']]
// Use dot-notation to find a specific element
$ reader -> value ( ' food.0 ' )-> sole (); // ['name' => 'Belgian Waffles', 'price' => '$5.95', ...]
// Use the element method to get a simple Element DTO containing attributes and content
$ reader -> element ( ' food.0 ' )-> sole (); // Element::class
// Use XPath to query the XML
$ reader -> xpathValue ( ' //food[@bestSeller="true"]/name ' )-> get (); // ['Belgian Waffles', 'Berry-Berry Belgian Waffles']
// Use getAttributes() to get the attributes on the elements
$ reader -> element ( ' food.0 ' )-> sole ()-> getAttributes (); // ['soldOut' => false, 'bestSeller' => true]
// Use getContent() to get the contents of the elements
$ reader -> element ( ' food.0 ' )-> sole ()-> getContent (); // ['name' => 'Belgian Waffles', 'price' => '$5.95', ...]
การเขียน XML นั้นง่ายพอ ๆ กับการกำหนดอาร์เรย์ PHP และการใช้คีย์และค่าเพื่อกำหนดองค์ประกอบ เมื่อคุณต้องการกำหนดองค์ประกอบที่มีคุณสมบัติอีกสองสามอย่างเช่นคุณลักษณะหรือเนมสเปซคุณสามารถใช้ Element
DTO เพื่อกำหนดองค์ประกอบขั้นสูงเพิ่มเติม
<?php
use Saloon XmlWrangler Data Element ;
use Saloon XmlWrangler XmlWriter ;
$ writer = new XmlWriter ;
$ xml = $ writer -> write ( ' breakfast_menu ' , [
' food ' => [
[
' name ' => ' Belgian Waffles ' ,
' price ' => ' $5.95 ' ,
' description ' => ' Two of our famous Belgian Waffles with plenty of real maple syrup ' ,
' calories ' => ' 650 ' ,
],
[
' name ' => ' Strawberry Belgian Waffles ' ,
' price ' => ' $7.95 ' ,
' description ' => ' Light Belgian waffles covered with strawberries and whipped cream ' ,
' calories ' => ' 900 ' ,
],
// You can also use the Element class if you need to define elements with
// namespaces or with attributes.
Element :: make ([
' name ' => ' Berry-Berry Belgian Waffles ' ,
' price ' => ' $8.95 ' ,
' description ' => ' Light Belgian waffles covered with an assortment of fresh berries and whipped cream ' ,
' calories ' => ' 900 ' ,
])-> setAttributes ([ ' bestSeller ' => ' true ' ]),
],
]);
รหัสด้านบนจะสร้าง XML ต่อไปนี้
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< breakfast_menu >
< food >
< name >Belgian Waffles</ name >
< price >$5.95</ price >
< description >Two of our famous Belgian Waffles with plenty of real maple syrup</ description >
< calories >650</ calories >
</ food >
< food >
< name >Strawberry Belgian Waffles</ name >
< price >$7.95</ price >
< description >Light Belgian waffles covered with strawberries and whipped cream</ description >
< calories >900</ calories >
</ food >
< food bestSeller = " true " >
< name >Berry-Berry Belgian Waffles</ name >
< price >$8.95</ price >
< description >Light Belgian waffles covered with an assortment of fresh berries and whipped cream</ description >
< calories >900</ calories >
</ food >
</ breakfast_menu >
ส่วนนี้ในเอกสารนี้ใช้สำหรับการใช้เครื่องอ่าน XML
เครื่องอ่าน XML สามารถยอมรับประเภทอินพุตที่หลากหลาย คุณสามารถใช้สตริง XML ไฟล์หรือจัดหาทรัพยากร นอกจากนี้คุณยังสามารถอ่าน XML ได้โดยตรงจากการตอบสนอง PSR (เช่นจาก Guzzle) หรือการตอบสนองของรถเก๋ง
use Saloon XmlWrangler XmlReader ;
$ reader = XmlReader :: fromString ( ' <?xml version="1.0" encoding="utf-8"?><breakfast_menu>... ' );
$ reader = XmlReader :: fromFile ( ' path/to/file.xml ' );
$ reader = XmlReader :: fromStream ( fopen ( ' path/to/file.xml ' , ' rb ' );
$ reader = XmlReader :: fromPsrResponse ( $ response );
$ reader = XmlReader :: fromSaloonResponse ( $ response );
คำเตือน เนื่องจากข้อ จำกัด ของคลาส PHP XMLREADER พื้นฐาน
fromStream
,fromPsrResponse
และวิธีการfromSaloon
จะสร้างไฟล์ชั่วคราวบนเครื่อง/เซิร์ฟเวอร์ของคุณเพื่ออ่านซึ่งจะถูกลบออกโดยอัตโนมัติเมื่อผู้อ่านถูกทำลาย คุณจะต้องตรวจสอบให้แน่ใจว่าคุณมีที่เก็บข้อมูลเพียงพอในเครื่องของคุณที่จะใช้วิธีนี้
คุณสามารถใช้ elements
และ values
เพื่อแปลงเอกสาร XML ทั้งหมดเป็นอาร์เรย์ หากคุณต้องการอาร์เรย์ของค่าให้ใช้วิธี values
- แต่ถ้าคุณต้องการเข้าถึงแอตทริบิวต์ในองค์ประกอบวิธี elements
จะส่งคืนอาร์เรย์ของ Element
DTOs
$ reader = XmlReader :: fromString (...);
$ elements = $ reader -> elements (); // Array of `Element::class` DTOs
$ values = $ reader -> values (); // Array of values.
หมายเหตุ หากคุณกำลังอ่านไฟล์ XML ขนาดใหญ่คุณควรใช้
element
หรือวิธีvalue
แทนแทน วิธีการเหล่านี้สามารถวนซ้ำผ่านไฟล์ XML ขนาดใหญ่โดยไม่ต้องใช้หน่วยความจำหมด
คุณสามารถใช้วิธี value
เพื่อรับค่าองค์ประกอบเฉพาะ คุณสามารถใช้ Dot-Notation เพื่อค้นหาองค์ประกอบของเด็ก นอกจากนี้คุณยังสามารถใช้จำนวนทั้งหมดเพื่อค้นหาตำแหน่งเฉพาะของหลายองค์ประกอบ วิธีนี้ค้นหาผ่านร่างกาย XML ทั้งหมดในวิธีที่มีประสิทธิภาพหน่วยความจำ
วิธีนี้จะส่งคืนคลาส LazyQuery
ซึ่งมีวิธีการที่แตกต่างกันเพื่อดึงข้อมูล
$ reader = XmlReader :: fromString ( '
<?xml version="1.0" encoding="utf-8"?>
<person>
<name>Sammyjo20</name>
<favourite-songs>
<song>Luke Combs - When It Rains It Pours</song>
<song>Sam Ryder - SPACE MAN</song>
<song>London Symfony Orchestra - Starfield Suite</song>
</favourite-songs>
</person>
' );
$ reader -> value ( ' person.name ' )-> sole () // ' Sammyjo20 '
$ reader -> value ( ' song ' )-> get (); // ['Luke Combs - When It Rains It Pours', 'Sam Ryder - SPACE MAN', ...]
$ reader -> value ( ' song.2 ' )-> sole (); // 'London Symfony Orchestra - Starfield Suite'
คุณสามารถใช้เมธอด element
เพื่อค้นหาองค์ประกอบเฉพาะ วิธีนี้จะให้คลาส Element
ซึ่งมีค่าและแอตทริบิวต์ คุณสามารถใช้ Dot-Notation เพื่อค้นหาองค์ประกอบของเด็ก นอกจากนี้คุณยังสามารถใช้จำนวนทั้งหมดเพื่อค้นหาตำแหน่งเฉพาะของหลายองค์ประกอบ วิธีนี้ค้นหาผ่านร่างกาย XML ทั้งหมดในวิธีที่มีประสิทธิภาพหน่วยความจำ
วิธีนี้จะส่งคืนคลาส LazyQuery
ซึ่งมีวิธีการต่าง ๆ ในการดึงข้อมูล
$ reader = XmlReader :: fromString ( '
<?xml version="1.0" encoding="utf-8"?>
<person>
<name>Sammyjo20</name>
<favourite-songs>
<song>Luke Combs - When It Rains It Pours</song>
<song>Sam Ryder - SPACE MAN</song>
<song>London Symfony Orchestra - Starfield Suite</song>
</favourite-songs>
</person>
' );
$ reader -> element ( ' name ' )-> sole (); // Element('Sammyjo20')
$ reader -> element ( ' song ' )-> get (); // [Element('Luke Combs - When It Rains It Pours'), Element('Sam Ryder - SPACE MAN'), ...]
$ reader -> element ( ' song.2 ' )-> sole (); // Element('London Symfony Orchestra - Starfield Suite')
บางครั้งมันง่ายกว่าที่จะสำรวจเอกสาร XML เมื่อคุณไม่ต้องกังวลเกี่ยวกับเนมสเปซและคำนำหน้าไปยังองค์ประกอบ หากคุณต้องการลบออกคุณสามารถใช้เมธอด removeNamespaces()
บนเครื่องอ่าน
$ reader = XmlReader :: fromString (...);
$ reader -> removeNamespaces ();
เมื่อค้นหาไฟล์ขนาดใหญ่คุณสามารถใช้วิธีการ lazy
หรือ collectLazy
ซึ่งจะส่งคืนเครื่องกำเนิดผลลัพธ์เพียงการเก็บรายการเดียวในหน่วยความจำในแต่ละครั้ง
$ names = $ reader -> element ( ' name ' )-> lazy ();
foreach ( $ names as $ name ) {
//
}
หากคุณใช้ Laravel คุณสามารถใช้วิธี collect
และ collectLazy
ซึ่งจะแปลงองค์ประกอบเป็นคอลเล็กชั่น Laravel/Lazy Collection หากคุณไม่ได้ใช้ Laravel คุณสามารถติดตั้งแพ็คเกจ illuminate/collections
ผ่านนักแต่งเพลงเพื่อเพิ่มฟังก์ชั่นนี้
$ names = $ reader -> value ( ' name ' )-> collect ();
$ names = $ reader -> value ( ' name ' )-> collectLazy ();
บางครั้งคุณอาจต้องการค้นหาองค์ประกอบหรือค่าเฉพาะที่องค์ประกอบมีแอตทริบิวต์เฉพาะ คุณสามารถทำได้โดยการให้อาร์กิวเมนต์ที่สองกับวิธี value
หรือ element
สิ่งนี้จะค้นหาองค์ประกอบสุดท้ายสำหรับแอตทริบิวต์และจะกลับมาหากตรงกับ
$ reader = XmlReader :: fromString ( '
<?xml version="1.0" encoding="utf-8"?>
<person>
<name>Sammyjo20</name>
<favourite-songs>
<song>Luke Combs - When It Rains It Pours</song>
<song>Sam Ryder - SPACE MAN</song>
<song recent="true">London Symfony Orchestra - Starfield Suite</song>
</favourite-songs>
</person>
' );
$ reader -> element ( ' song ' , [ ' recent ' => ' true ' ])-> sole (); // Element('London Symfony Orchestra - Starfield Suite')
$ reader -> value ( ' song ' , [ ' recent ' => ' true ' ])-> sole (); // 'London Symfony Orchestra - Starfield Suite'
XPath เป็นวิธีที่ยอดเยี่ยมในการค้นหาผ่าน XML ด้วยหนึ่งสตริงคุณสามารถค้นหาองค์ประกอบเฉพาะด้วยแอตทริบิวต์หรือดัชนีเฉพาะ หากคุณสนใจที่จะเรียนรู้ XPATH คุณสามารถคลิกที่นี่เพื่อหา Cheatsheet ที่มีประโยชน์
คุณสามารถใช้วิธี xpathValue
เพื่อค้นหาค่าองค์ประกอบเฉพาะด้วยการสืบค้น XPath วิธีนี้จะส่งคืนคลาส Query
ซึ่งมีวิธีการต่าง ๆ เพื่อดึงข้อมูล
<?php
$ reader = XmlReader :: fromString (...);
$ reader -> xpathValue ( ' //person/favourite-songs/song[3] ' )-> sole (); // 'London Symfony Orchestra - Starfield Suite'
คุณสามารถใช้วิธีการ xpathElement
เพื่อค้นหาองค์ประกอบเฉพาะที่มีการสืบค้น XPATH วิธีนี้จะส่งคืนคลาส Query
ซึ่งมีวิธีการต่าง ๆ เพื่อดึงข้อมูล
<?php
$ reader = XmlReader :: fromString (...);
$ reader -> xpathElement ( ' //person/favourite-songs/song[3] ' )-> sole (); // Element('London Symfony Orchestra - Starfield Suite')
คำเตือน เนื่องจากข้อ จำกัด ของ XPATH - วิธีการข้างต้นที่ใช้ในการสืบค้นกับ XPATH นั้นไม่ปลอดภัยกับหน่วยความจำและอาจไม่เหมาะสำหรับเอกสาร XML ขนาดใหญ่
คุณอาจพบว่าตัวเองมีเอกสาร XML ที่มีแอตทริบิวต์ xmlns
ที่ไม่ได้รับการคัดเลือก - เช่นนี้:
< container xmlns = " http://example.com/xml-wrangler/person " xmlns : xsi = " http://www.w3.org/2001/XMLSchema-instance " />
เมื่อสิ่งนี้เกิดขึ้น XML Wrangler จะลบเนมสเปซที่ไม่ได้รับการคัดเลือกเหล่านี้โดยอัตโนมัติเพื่อปรับปรุงความเข้ากันได้ หากคุณต้องการเก็บเนมสเปซเหล่านี้คุณสามารถใช้ setXpathNamespaceMap
เพื่อแมปเนมสเปซ XML ที่ไม่ได้รับการคัดเลือกแต่ละตัว
$ reader = XmlReader :: fromString (...);
$ reader -> setXpathNamespaceMap ([
' root ' => ' http://example.com/xml-wrangler/person ' ,
]);
$ reader -> xpathValue ( ' //root:person/root:favourite-songs/root:song[3] ' )-> sole ();
ส่วนนี้ในเอกสารนี้มีไว้สำหรับการใช้นักเขียน XML
การใช้งานพื้นฐานที่สุดของผู้อ่านคือการใช้คีย์สตริงสำหรับชื่อองค์ประกอบและค่าสำหรับค่าขององค์ประกอบ ผู้เขียนยอมรับอาร์เรย์ซ้อนกันอย่างไม่มีที่สิ้นสุดสำหรับองค์ประกอบที่ซ้อนกัน
use Saloon XmlWrangler XmlWriter ;
$ xml = XmlWriter :: make ()-> write ( ' root ' , [
' name ' => ' Sam ' ,
' twitter ' => ' @carre_sam ' ,
' facts ' => [
' favourite-song ' => ' Luke Combs - When It Rains It Pours '
],
]);
รหัสข้างต้นจะถูกแปลงเป็น XML ต่อไปนี้
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< root >
< name >Sam</ name >
< twitter >@carre_sam</ twitter >
< facts >
< favourite-song >Luke Combs - When It Rains It Pours</ favourite-song >
</ facts >
</ root >
เมื่อเขียน XML คุณมักจะต้องกำหนดคุณลักษณะและเนมสเปซในองค์ประกอบของคุณ คุณสามารถใช้คลาส Element
ในอาร์เรย์ของ XML เพื่อเพิ่มองค์ประกอบด้วยแอตทริบิวต์หรือเนมสเปซ คุณสามารถผสมคลาส Element
กับอาร์เรย์อื่น ๆ และค่าสตริง
use Saloon XmlWrangler XmlWriter ;
use Saloon XmlWrangler Data Element ;
$ xml = XmlWriter :: make ()-> write ( ' root ' , [
' name ' => ' Sam ' ,
' twitter ' => Element :: make ( ' @carre_sam ' )-> addAttribute ( ' url ' , ' https://twitter.com/@carre_sam ' ),
' facts ' => [
' favourite-song ' => ' Luke Combs - When It Rains It Pours '
],
' soap:custom-namespace ' => Element :: make ()-> addNamespace ( ' soap ' , ' http://www.w3.org/2003/05/soap-envelope ' ),
]);
ซึ่งจะส่งผลให้ XML ต่อไปนี้
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< root >
< name >Sam</ name >
< twitter url = " https://twitter.com/@carre_sam " >@carre_sam</ twitter >
< facts >
< favourite-song >Luke Combs - When It Rains It Pours</ favourite-song >
</ facts >
< soap : custom-namespace xmlns : soap = " http://www.w3.org/2003/05/soap-envelope " />
</ root >
คุณมักจะต้องกำหนดองค์ประกอบอาร์เรย์ คุณสามารถทำได้โดยเพียงแค่จัดทำอาร์เรย์ของค่าหรือคลาสองค์ประกอบ
use Saloon XmlWrangler XmlWriter ;
use Saloon XmlWrangler Data Element ;
$ xml = XmlWriter :: make ()-> write ( ' root ' , [
' name ' => ' Luke Combs ' ,
' songs ' => [
' song ' => [
' Fast Car ' ,
' The Kind Of Love We Make ' ,
' Beautiful Crazy ' ,
Element :: make ( ' She Got The Best Of Me ' )-> addAttribute ( ' hit ' , ' true ' ),
],
],
]);
ซึ่งจะส่งผลให้ XML ต่อไปนี้
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< root >
< name >Luke Combs</ name >
< songs >
< song >Fast Car</ song >
< song >The Kind Of Love We Make</ song >
< song >Beautiful Crazy</ song >
< song hit = " true " >She Got The Best Of Me</ song >
</ songs >
</ root >
บางครั้งคุณอาจต้องเปลี่ยนชื่อขององค์ประกอบรูท สิ่งนี้สามารถปรับแต่งเป็นอาร์กิวเมนต์แรกของวิธี write
$ xml = XmlWriter :: make ()-> write ( ' custom-root ' , [...])
หากคุณต้องการเพิ่มแอตทริบิวต์และเนมสเปซลงในองค์ประกอบรูทคุณสามารถใช้คลาส RootElement
ที่นี่ได้เช่นกัน
use Saloon XmlWrangler Data RootElement ;
$ rootElement = RootElement :: make ( ' root ' )-> addNamespace ( ' soap ' , ' http://www.w3.org/2003/05/soap-envelope ' );
$ xml = XmlWriter :: make ()-> write ( $ rootElement , [...])
หากคุณต้องการเพิ่มแท็ก CDATA คุณสามารถใช้คลาส CDATA
use Saloon XmlWrangler Data CDATA ; use Saloon XmlWrangler XmlWriter ;
use Saloon XmlWrangler Data Element ;
$ xml = XmlWriter :: make ()-> write ( ' root ' , [
' name ' => ' Sam ' ,
' custom ' => CDATA :: make ( ' Here is some CDATA content! ' ),
]);
ซึ่งจะส่งผลให้ XML ต่อไปนี้
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< root >
< name >Sam</ name >
< custom > <![CDATA[ Here is some CDATA content! ]]> </ custom >
</ root >
บางครั้งคุณอาจมีส่วนหนึ่งของ XML ที่คุณจะนำกลับมาใช้ใหม่ในคำขอ XML หลายรายการในแอปพลิเคชันของคุณ ด้วย XML Wrangler คุณสามารถสร้างองค์ประกอบ "composable" ที่คุณสามารถกำหนดเนื้อหา XML ของคุณในชั้นเรียนที่คุณสามารถใช้ซ้ำในแอปพลิเคชันของคุณ ขยายคลาส Element
และใช้วิธี compose
แบบสแตติกที่ได้รับการป้องกัน
<?php
use Saloon XmlWrangler XmlWriter ;
use Saloon XmlWrangler Data Element ;
class BelgianWafflesElement extends Element
{
protected function compose (): void
{
$ this
-> setAttributes ([
' soldOut ' => ' false ' ,
' bestSeller ' => ' true ' ,
])
-> setContent ([
' name ' => ' Belgian Waffles ' ,
' price ' => ' $5.95 ' ,
' description ' => ' Two of our famous Belgian Waffles with plenty of real maple syrup ' ,
' calories ' => ' 650 ' ,
]);
}
}
$ writer = XmlWriter :: make ()-> write ( ' root ' , [
' food ' => new BelgianWafflesElement ,
]);
ซึ่งจะส่งผลให้ XML เช่นนี้:
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< breakfast_menu >
< food soldOut = " false " bestSeller = " true " >
< name >Belgian Waffles</ name >
< price >$5.95</ price >
< description >Two of our famous Belgian Waffles with plenty of real maple syrup</ description >
< calories >650</ calories >
</ food >
</ breakfast_menu >
การเข้ารหัส XML เริ่มต้นคือ utf-8
รุ่นเริ่มต้นของ XML คือ 1.0
และสแตนด์อโลนเริ่มต้นคือ null
(XML parsers ตีความไม่มีแอตทริบิวต์แบบสแตนด์อโลนเหมือนกับ false
) หากคุณต้องการปรับแต่งสิ่งนี้คุณสามารถทำได้ด้วยวิธีการ setXmlEncoding
, setXmlVersion
และ setXmlStandalone
บนผู้เขียน
use Saloon XmlWrangler XmlWriter ;
$ writer = new XmlWriter ();
$ writer -> setXmlEncoding ( ' ISO-8859-1 ' );
$ writer -> setXmlVersion ( ' 2.0 ' );
$ writer -> setXmlStandalone ( true );
// $writer->write(...);
ผลลัพธ์ใดในการประกาศ XML <?xml version="2.0" encoding="ISO-8859-1" standalone="yes"?>
คุณสามารถเพิ่ม "คำสั่งการประมวลผล" ที่กำหนดเองให้กับ XML โดยใช้วิธี addProcessingInstruction
use Saloon XmlWrangler XmlWriter ;
$ writer = new XmlWriter ();
$ writer -> addProcessingInstruction ( ' xml-stylesheet ' , ' type="text/xsl" href="base.xsl" ' );
$ xml = $ writer -> write ( ' root ' , [ ' name ' => ' Sam ' ]);
ซึ่งจะส่งผลให้ XML ต่อไปนี้
<? xml version = " 1.0 " encoding = " utf-8 " ?>
<? xml-stylesheet type = " text/xsl " href = " base.xsl " ?>
< root >
< name >Sam</ name >
</ root >
โดยค่าเริ่มต้นการเขียน XML จะไม่ลดลง คุณสามารถให้อาร์กิวเมนต์ที่สามกับวิธี write
เพื่อลด XML
use Saloon XmlWrangler XmlWriter ;
$ xml = XmlWriter :: make ()-> write ( ' root ' , [...], minified: true );
XML Wrangler เป็นเสื้อคลุมที่เรียบง่ายประมาณสองห้องสมุดที่ทรงพลังจริงๆซึ่งทำงานหนักมาก ห้องสมุดทั้งสองนั้นยอดเยี่ยมและสมควรได้รับดาว!