交叉码 / xml-to-object
将XML转换为对象
1.0.0
2019-11-15 11:42 UTC
Requires
- php: ^7.0
This package is auto-updated.
Last update: 2024-09-15 22:33:33 UTC
README
这个库简单地实现了将XML转换为对象的功能。
入门指南
XML到对象的转换非常简单易用。 示例:
// Using composer here, but you can also directly include XmlToObject.php require_once __DIR__ . '/vendor/autoload.php'; // Import the function use function KrossCode\XmlToObject\xmlToObject; // First we need to get the contents of our XML file $xmlContents = file_get_contents('path/to/xml/file.xml'); if (!$xmlContents) return false; // File couldn't be read // Now we try to convert the XML to an object $xmlObject = xmlToObject($xmlContents); if (!$xmlObject) return false; // XML couldn't be converted to an object print_r($xmlObject); // Success!
示例
与XML相比,对象看起来是怎样的?
XML
<GroceryStore xmlns="https://example.com/schema/base" xmlns:ns3="https://example.com/schema/ns3"> <Name>Johnny's Green Goods</Name> <GroceryCollection> <ns3:Grocery id="13"> <Name>Cucumber</Name> <Amount>63</Amount> <Price>1.99</Price> </ns3:Grocery> <ns3:Grocery id="17"> <Name>Carrot</Name> <Amount>24</Amount> <Price>1.79</Price> </ns3:Grocery> </GroceryCollection> </GroceryStore>
对象(以JSON表示)
{
"Name": "Johnny's Green Goods",
"GroceryCollection": {
"Grocery": [
{
"@attributes": {
"id": "13"
},
"Name": "Cucumber",
"Amount": "63",
"Price": "1.99"
},
{
"@attributes": {
"id": "17"
},
"Name": "Carrot",
"Amount": "24",
"Price": "1.79"
}
]
}
}