<< xmlNs Traitement XML xmlRead >>

Scilab Help >> Traitement XML > XML Objects

XML Objects

Describe the properties of the different XML objects

Contents

Description

The nodes and their properties can be accessed and modified.

XML Document

A XML Document has two properties: root and url

doc = xmlReadStr("<root><a att=""foo"" rib=""bar""><b>Hello</b></a></root>");
doc.root = doc.root.children(1);
xmlDump(doc)
doc.url = TMPDIR+"/foo.xml";
doc
xmlWrite(doc);
xmlDelete(doc);

XML Element

A XML Element has seven properties:

s = "<root xmlns:bar=""http://www.scilab.org/"">"+..
    "<bar:a att=""foo"" rib=""bar"">"+..
    "<b>Hello</b><c> world</c></bar:a></root>"
doc = xmlReadStr(s);
first = doc.root.children(1);
b = first.children(1);

// Add a new attribute named "new_attribute"
first.attributes.new_attribute = "value";

// Display the first child namespace
first.namespace

// Display the node content
first.content

// b has a parent
b.parent

// You can add a new child to first
first.children(3) = b

// non-integer index can be used to make insertion
first.children(1.5) = "<d> Scilab</d>"

// First child has been defined at line...
b.line

xmlDump(first)
xmlDelete(doc);

XML Attributes

A XML Attributes is a kind of hashtable which maps attributes name to attributes value. An attribute value can be accessed or modified in using the attribute name as field of this object or in using an index between 1 and attributes size.

s = "<root xmlns:bar=""http://www.scilab.org/"">"+..
    "<bar:a att=""foo"" rib=""bar"">"+..
    "<b>Hello</b><c> world</c></bar:a></root>"
doc = xmlReadStr(s);
first = doc.root.children(1);

// Read an attribute
first.attributes.att

// Set an empty attribute
first.attributes.att = "";

// Add a new attribute
first.attributes.hello = "world";

// Use an index
first.attributes(1) = "Bonjour";
first.attributes(1)

xmlDump(first)
xmlDelete(doc);

XML Namespace

A XML Namespace has two properties: href and prefix

s = "<root xmlns:bar=""http://www.scilab.org/"">"+..
    "<bar:a att=""foo"" rib=""bar"">"+..
    "<b>Hello</b><c> world</c></bar:a></root>"
doc = xmlReadStr(s);
ns = doc.root.children(1).namespace;
ns.href
ns.prefix

xmlDelete(doc);

XML Node List

A XML Node List is a type used to enumerate the children of an element. Each element can be accessed with an integer index. Since this is a list, it is possible to make insertion of new element in it, in using double index.

The size of the list can be retrieved in using 'size' field.

The name or the contents of each node of the list can be retrieved in using 'name' or 'content' field.

doc = xmlReadStr("<root><a>Hello</a><b> world</b></root>");
c = doc.root.children;

// We check that we have two elements
c.size

// Read the first element
xmlDump(c(1))

// Replace an element by another one
c(1) = "<c>Hello</c>"

// Insert a new element between the first and the second
c(1.5) = "<d> Scilab</d>" // 1.5 or 1.234...

// Insert a new element at the tail or at the head of the list
c(0) = "<e>Head </e>"
c(217) = "<f> Tail</f>"

xmlDump(c)

// Get the nodes name
c.name

// Get the nodes contents
c.content

xmlDelete(doc);

XML XPath result set

A XML Node Set is an object returned by a XPath query. It is not possible to insert new elements or to replace existing ones. It is just possible to get them in using an integer index.

The size of the set can be retrieved with field 'size'.

doc = xmlReadStr("<root><a><b>Hello</b></a><a>World</a></root>");
s = xmlXPath(doc, "//a")
s.size

s(1).content
s(2).content

// Or ...
s.content

xmlDelete(doc);

XML Validation file

A XML Validation file is an object used to validate a document. It is possible to validate in using DTD, Relax NG or schema.

doc = xmlRead("SCI/modules/xml/tests/unit_tests/library.xml");
dtd = xmlDTD("SCI/modules/xml/tests/unit_tests/library.dtd");
schema = xmlSchema("SCI/modules/xml/tests/unit_tests/library.xsd");
rng = xmlRelaxNG("SCI/modules/xml/tests/unit_tests/library.rng");

// Validation
xmlValidate(doc, dtd);
xmlValidate(doc, rng);
xmlValidate(doc, schema);

xmlDelete("all");

See also

History

VersionDescription
5.4.0 XML module introduced.

Report an issue
<< xmlNs Traitement XML xmlRead >>