Skip to content

Commit 09c3fdf

Browse files
committed
Adding ability to write out attributes with "writeElement"
1 parent c421da9 commit 09c3fdf

File tree

4 files changed

+214
-102
lines changed

4 files changed

+214
-102
lines changed

README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Learn more about Composer here: <a href="https://getcomposer.org/">https://getco
2424
To get started creating your own XML document:
2525

2626
```php
27+
<?php
28+
2729
use \DCarbone\XMLWriterPlus;
2830

2931
// Initialize writer instance
@@ -46,6 +48,9 @@ $xmlWriterPlus->text('Root element node value');
4648
// This method opens, writes value, and closes an element all in one go
4749
$xmlWriterPlus->writeElement('Child', 'Root element child element');
4850

51+
// Append a child element with some attributes in one go
52+
$xmlWriterPlus->writeElement('AttributedChild', 'some data', ['attr' => 'value!', 'ns:attr' => 'other value!']);
53+
4954
// Insert a CDATA element
5055
$xmlWriterPlus->writeCDataElement('MyCData', '<div>This div won\'t confuse XML Parsers! <br></div>');
5156

@@ -62,7 +67,8 @@ echo htmlspecialchars($xmlWriterPlus->outputMemory());
6267
The above will output:
6368

6469
```xml
65-
<?xml version="1.0" encoding="UTF-8"?> <!--This is a comment and it contains superfluous information--><Root>Root element node value<Child>Root element child element</Child><MyCData><![CDATA[<div>This div won't confuse XML Parsers! <br></div>]]></MyCData></Root>
70+
<?xml version="1.0" encoding="UTF-8"?>
71+
<!--This is a comment and it contains superfluous information--><Root>Root element node value<Child>Root element child element</Child><AttributedChild attr="value!" ns:attr="other value!">some data</AttributedChild><MyCData><![CDATA[<div>This div won't confuse XML Parsers! <br></div>]]></MyCData></Root>
6672
```
6773

6874
Or, more legibly,
@@ -71,10 +77,9 @@ Or, more legibly,
7177
<?xml version="1.0" encoding="UTF-8"?>
7278
<!--This is a comment and it contains superfluous information-->
7379
<Root>Root element node value
74-
<Child>Root element child element</Child>
75-
<MyCData>
76-
<![CDATA[<div>This div won't confuse XML Parsers! <br></div>]]>
77-
</MyCData>
80+
<Child>Root element child element</Child>
81+
<AttributedChild attr="value!" ns:attr="other value!">some data</AttributedChild>
82+
<MyCData><![CDATA[<div>This div won't confuse XML Parsers! <br></div>]]></MyCData>
7883
</Root>
7984
```
8085

composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
],
1818
"require": {
1919
"php": ">=5.4.0",
20-
"lib-libxml": "*"
20+
"ext-libxml": "*",
21+
"ext-xmlwriter": "*",
22+
"ext-mbstring": "*"
2123
},
2224
"require-dev": {
2325
"phpunit/phpunit": "<6.0"
@@ -26,5 +28,9 @@
2628
"psr-4": {
2729
"DCarbone\\": "src/"
2830
}
31+
},
32+
"suggest": {
33+
"ext-simplexml": "Enable export to \\SimpleXMLElement",
34+
"ext-dom": "Enable export to \\DOMDocument"
2935
}
3036
}

0 commit comments

Comments
 (0)