GO2cam Javascript API  6.11
GO2SXmlFile Class Reference

Class to manage XML files. More...

Inheritance diagram for GO2SXmlFile:
GO2SFile

Public Member Functions

 GO2SXmlFile (String fileName, GO2SEnum::GO2SFileOpen mode=GO2SEnum::GO2SFileOpen::read_only)
 Open and load a .xml file.
 
GO2SEnum::GO2SError AppendNode (String nodeName)
 Append a new empty node.
 
GO2SEnum::GO2SError CreateRootNode (String nodeName)
 Create start node.
 
Boolean GoToChild ()
 
Boolean GoToParent ()
 Go to parent if exists.
 
void GoToRoot ()
 Go to start node of the file.
 
Boolean GoToSibling ()
 
Array< Array< String > > ReadAttributes ()
 
String ReadAttributeValue (String attributeName)
 
String ReadChildValue (String childName)
 
String ReadNodeName ()
 
String ReadNodeValue ()
 
GO2SEnum::GO2SError SetNodeAttribute (String attributeName, String newValue)
 
GO2SEnum::GO2SError SetNodeValue (String text)
 Add a value to the current node.
 
- Public Member Functions inherited from GO2SFile
void Close ()
 Close the file.
 
String GetFilePath ()
 Get the full path (directory + name) of the file.
 
Boolean IsOpen ()
 Get if the file is open.
 
void Save ()=0
 

Detailed Description

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

Member Function Documentation

◆ AppendNode()

GO2SEnum::GO2SError GO2SXmlFile::AppendNode ( String nodeName)

Append a new empty node.

Parameters
[in]nodeNamename of the node

◆ CreateRootNode()

GO2SEnum::GO2SError GO2SXmlFile::CreateRootNode ( String nodeName)

Create start node.

Parameters
[in]nodeNamename of the root node

◆ GoToChild()

Boolean GO2SXmlFile::GoToChild ( )

Go to the next child node if exists

Returns
true if next child exists, false otherwise

◆ GoToSibling()

Boolean GO2SXmlFile::GoToSibling ( )

Go to the next sibling if exists

Returns
true if next sibling exists, false otherwise

◆ ReadAttributes()

Array< Array< String > > GO2SXmlFile::ReadAttributes ( )

Read all attributes For example

<Screw Size = "1.698" Length = "4.6"/> ReadAttributes() returns { { Size, 1.698 }, { Length, 4.6 } }
Array< Array< String > > ReadAttributes()

◆ ReadAttributeValue()

String GO2SXmlFile::ReadAttributeValue ( String attributeName)

Read the value of the attribute with the given name For example

<Screw Size = "1.698" Length = "4.6"/> ReadAttributeValue('Length') returns '4.6'
String ReadAttributeValue(String attributeName)
Parameters
[in]attributeNamename of the attribute to read

◆ ReadChildValue()

String GO2SXmlFile::ReadChildValue ( String childName)

Read the value contained in the child node having the given name Read the value contained in the node For example

<Instr>
<Provider>Gibson</Provider>
<Model>Les Paul</Provider>
<Instr>

When on <Instr> node, ReadChildValue(Model) returns 'Les Paul'

Parameters
[in]childNamechild name

◆ ReadNodeName()

String GO2SXmlFile::ReadNodeName ( )
inline

Read the name of the current node For example

<Provider>Gibson</Provider> the node name is Provider

◆ ReadNodeValue()

String GO2SXmlFile::ReadNodeValue ( )

Read the value contained in the node For example

<Provider>Gibson</Provider> the node value is Gibson

◆ SetNodeAttribute()

GO2SEnum::GO2SError GO2SXmlFile::SetNodeAttribute ( String attributeName,
String newValue )

Add an attribute to the current node

Remarks
if the attribute already exists, his value is replaced by the new one
Parameters
[in]attributeNamename of the attribute
[in]newValuevalue of the attribute

◆ SetNodeValue()

GO2SEnum::GO2SError GO2SXmlFile::SetNodeValue ( String text)

Add a value to the current node.

Parameters
[in]textvalue of the node