Laravel XML Reader & Writer
- Base Methods
- Exporting arrays
- Exporting a array without keys
- [Changing the
- name](#changing-the-item-name)
- [Using a custom
and - name.](#using-a-custom-root-and-item-name)
- Exporting views
- Pretty output
Exporting data to XML
An easy way to export your data to XML is using the XML::export
method. This method can load views or translate arrays to XML.
Base Methods
Some important methods you need to know about.
- setRootTag(
string $name
) or rootTag(string $name
) - if you want to change the tag name. Defaultexport
- version(
string $version
) - if you want to change the xml version. Default1.0
- encoding(
string $encoding
) - if you want to change the xml encoding. DefaultUTF-8
- toString() - if you want to get the xml output as a string
- toFile(
string $path
) - if you want to save the xml to a file directly. - forceItemName() - if you want to disable default item name generation
- disableRootTag() - if you want to disable the root tag
Exporting arrays
To export a array to xml you need to use the export()
method.
$data = [
'file' => [
[
'name' => 'file1',
'type' => 'pdf',
],
[
'name' => 'file2',
'type' => 'png',
],
[
'name' => 'file3',
'type' => 'xml',
],
],
];
$xml = XML::export($data)
->toString();
This produces the following xml as a string
<?xml version="1.0" encoding="UTF-8"?>
<files>
<file>
<name>file1</name>
<type>pdf</type>
</file>
<file>
<name>file2</name>
<type>png</type>
</file>
<file>
<name>file3</name>
<type>xml</type>
</file>
</files>
If you want to save it as a file simply replace toString()
with toFile("/my/path/file.xml")
Exporting a array without keys
Version 2 of the package makes it possible to export simple arrays that do not have keys.
$data = [
'file1',
'file2',
'file3',
];
$xml = XML::export($data);
Would create
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>file1</item>
<item>file2</item>
<item>file3</item>
</root>
Changing the - name
If you want to change to item name set it using ->itemName($name)
.
$data = [
'file1',
'file2',
'file3',
];
$xml = XML::export($data)
->itemName('file');
Would create
<?xml version="1.0" encoding="UTF-8"?>
<root>
<file>file1</file>
<file>file2</file>
<file>file3</file>
</root>
Using a custom and - name.
By default the default item name is based on a singular case of the root tag. If the root tag is “root” we will use “item” as the default item name.
If you are using a custom root tag like “files” we would set the default item name to “file”.
To set your own item name use ->itemName($name)
and then ->forceItemName()
.
$data = [
'file1',
'file2',
'file3',
];
$xml = XML::export($data)
->rootTag('user_files')
->itemName('file');
Would create
<?xml version="1.0" encoding="UTF-8"?>
<user_files>
<user_file>file1</user_file>
<user_file>file2</user_file>
<user_file>file3</user_file>
</user_files>
$data = [
'file1',
'file2',
'file3',
];
$xml = XML::export($data)
->rootTag('user_files')
->itemName('file')
->forceItemName();
Would create
<?xml version="1.0" encoding="UTF-8"?>
<user_files>
<file>file1</file>
<file>file2</file>
<file>file3</file>
</user_files>
Exporting views
To export a view simply call exportView($viewName, $data = [])
$xml = XML::exportView('my-view', [])
->toString();
Pretty output
By default, the exported XML will be minified. To get the XML in a pretty format pass true
to the toString()
method.
Or you can use usePrettyOutput()
when using the toFile()
method.
$xml = XML::export($data)
->toString(true);
XML::export($data)
->usePrettyOutput()
->toFile($filename);