Replies: 3 comments
-
|
Hi dear @denormalizer. What you want to do can be easily done using relative XPath queries; I've implemented them especially for this reason. $xml = new FluidXml('food');
$xml->addChild('menu', true)
->addChild('pasta', true)
->addChild('name', 'Spaghetti')
->query('..')
->addChild('pasta', true)
->addChild('name', 'Fettuccine');which produces exactly what you want <food>
<menu>
<pasta>
<name>Spaghetti</name>
</pasta>
<pasta>
<name>Fusilli</name>
</pasta>
</menu>
</food>If I can give you a suggestion, when you have a predictable data structure which you want to convert to XML, prefer the array input format. $xml = new FluidXml('food');
$data = [ 'menu' => [
[ 'pasta' => [ 'name' => 'spaghetti' ] ],
[ 'pasta' => [ 'name' => 'fettuccine' ] ],
]];
$xml->addChild($data);which is more readable, easier to generalise and to refactor. |
Beta Was this translation helpful? Give feedback.
-
|
Hi Daniele, Thanks for your suggestion. I've been umming and ahhing over which method to go with. I originally went with the array method then I switched over to the fluid method. I have a predictable (but configurable) data structure. I also have dynamic data. That means I want to be able to easily replace spaghetti with bolognese How would you do it with the array method? |
Beta Was this translation helpful? Give feedback.
-
|
I would generate a dynamic array. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The
switchContextparameter allows a node to add a sibling node to the current node or a child node to the current node.Is it possible to use the fluent method to add a sibling node to the parent node?
For example, I would like to produce the following
Case where
switchContextis false for Spaghetti node:would produce the following:
Case where
switchContextis true for Spaghetti node:would produce the following:
As you can see, neither produces the correct XML.
Question: Is there a way to do this using add/Child() alone? Or do I need to chain xpath queries prior to adding the nodes?
Beta Was this translation helpful? Give feedback.
All reactions