Class to manage XML files.
Exemple:
var filename = 'U:/test.xml';
var file = new GO2SXmlFile(filename, GO2SFileOpen.read_write);
// create nodes
file.CreateRootNode('TestFile');
file.AppendNode('Node1');
file.AppendNode('Node2');
file.GoToChild(); // to Node1
file.GoToSibling() // to Node2
file.AppendNode('SubNode1');
file.AppendNode('SubNode2');
file.GoToParent() // to root
file.AppendNode('Node3');
// write node content
file.GoToRoot() // to root
// write Node1
file.GoToChild() //to Node1
file.SetNodeValue('ValueNode1');
// write Node2
file.GoToSibling() //to Node2
file.GoToChild(); // to SubNode1
file.SetNodeValue('ValueSubNode1');
file.GoToSibling() // to SubNode2
file.SetNodeValue('ValueSubNode2');
file.GoToParent() //to Node2
// write Node3
file.GoToSibling(); // to Node3
file.SetNodeAttribute('att1', 'val1');
file.SetNodeAttribute('att2', 'val2');
file.SetNodeAttribute('att3', 'val2');
file.Save();
file.Close();
the previous code will create the xml file:
<?xml version="1.0" encoding="UTF-8"?>
<TestFile>
<Node1>ValueNode1</Node1>
<Node2>
<SubNode1>ValueSubNode1</SubNode1>
<SubNode2>ValueSubNode2</SubNode2>
</Node2>
<Node3 att1="val1" att2="val2" att3="val2"/>
</TestFile>
The following code reads it
let file = new GO2SXmlFile('u:/test.xml')
file.GoToChild() // go to node1
let node = file.ReadNodeName()
let val = file.ReadNodeValue()
console.log(node + ' = ' + val)
file.GoToSibling() // go to node2
file.GoToChild() // go to subnode
node = file.ReadNodeName()
val = file.ReadNodeValue()
console.log(node + ' = ' + val)
file.GoToParent() // back to node2
node = 'SubNode2'
val = file.ReadChildValue(node)
console.log(node + ' = ' + val)
file.GoToSibling() // go to node3
node = file.ReadNodeName()
val = file.ReadAttributes()
console.log(node + ' = ' + val)
node = 'att1'
val = file.ReadAttributeValue(node)
console.log(node + ' = ' + val)
output :
Node1 = ValueNode1
SubNode1 = ValueSubNode1
SubNode2 = ValueSubNode2
Node3 = att3, val2, att1, val1, att2, val2
att1 = val1