-
Is there any way to insert nodes directly in xml file without recreating full xml document with builder? <rootNode>
...
<mainNode>
...
<randomNode>111</randomNode>
<randomNode2>1</randomNode2>
<randomNode3><subNode1><dataId>1</dataId></subNode1></randomNode3>
<item name="Text1"><dataValue>1</dataValue></item >
<item name="Text2"><dataValue>2</dataValue></item >
<item name="Text3"><dataValue>3</dataValue></item >
>>>>>> insert here ( example <item name="Text4"><dataValue>4</dataValue></item > ) <<<<<<
<subNode2><child>text text</child></subNode2>
...
<mainNode>
...
</rootNode> |
Beta Was this translation helpful? Give feedback.
Answered by
renggli
Jan 18, 2022
Replies: 2 comments 4 replies
-
Sure, something along those lines should work: final document = XmlDocument.parse(input);
final mainNode = document.findAllElements('mainNode').first;
final lastItemNode = mainNode.findAllElements('item').last;
final lastItemIndex = mainNode.children.indexOf(lastItemNode);
final builder = XmlBuilder();
builder.element('item',
attributes: {'name': 'Text4'},
nest: () => builder.element('dataValue', nest: 4));
mainNode.children.insert(lastItemIndex + 1, builder.buildFragment());
final output = document.toXmlString(); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mitien
-
Correct, see builder.dart#L252. If you want multiple instances you can copy the built tree: final item = builder.buildFragment();
final copy = item.copy(); |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure, something along those lines should work: