Xml Module

From SolarStrike wiki
Revision as of 05:01, 17 November 2008 by 12.201.75.247 (talk) (New page: == xml.open == '''xml.open(filename)''' Opens and parses an XML file, and returns a class containing the root node. '''Example''' <source lang="xml"> <root name="Root" type="Example"> <...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

xml.open

xml.open(filename)

Opens and parses an XML file, and returns a class containing the root node.

Example

<root name="Root" type="Example">
	<elem1>
		<subelement value="1" />
		<subelement value="2" />
		<subelement value="3" />
	</elem1>

	<elem2 number="1234" string="This is a string" />

	<elem3>Element 3 has a text value</elem3>

	<elem4 num="123">999</elem4>
</root>
function main()
  local root = xml.open(getExecutionPath() .. "/test.xml");
  printf("root._NAME: %s\n", root:getName()); -- "_NAME" should be 'root'
  printf("root.name: %s\n", root:getAttribute("name"));    -- "name" will be set in XML
  printf("root.attribute count: %d\n", root:getAttributeCount());

  local elem1 = root:getElement("elem1");
  elements = elem1:getElements();
  printf("\n---ELEM1---\n");
  for i,v in pairs(elements) do
    local name = v:getName();
    local value = v:getAttribute("value");
    printf(" ==> [%s]: %s\n", name, value);
  end


  local elem2 = root:getElement("elem2");
  printf("\n---ELEM2---\n");
  printf("elem2.number: %d\n", elem2:getAttribute("number"));
  printf("elem2.string: %s\n", elem2:getAttribute("string"));

  local elem3 = root:getElement("elem3");
  printf("\n---ELEM3---\n");
  printf("elem3._VALUE: %s\n", elem3:getValue());

  local elem4 = root:getElement("elem4");
  printf("\n---ELEM4---\n");
  local val = elem4:getValue();
  local num = elem4:getAttribute("num");

  if( num == nil ) then
    printf("num is nil\n");
  end

  printf("elem4.num(type): %s\n", type(num));
  printf("elem4.num: %d\n", num);

  printf("elem4._VALUE(type): %s\n", type(val));
  printf("elem4._VALUE: %d\n", val);
end
startMacro(main);


XML Node class

Node:getElement(name)

Node:getElement(index)

Return an element (node) inside this node with the name 'name' or index 'index'.


Node:getElements()

Returns all elements (nodes) inside this node.


Node:getElementCount()

Returns the number of elements (nodes) inside this node.


Node:getAttribute(name)

Node:getAttribute(index)

Returns an attribute for this node with the name 'name' or index 'index'.


Node:getAttributes()

Returns all attributes for this node.


Node:getAttributeCount()

Returns the number of attributes for this node.


Node:getName()

Returns the name of this node.


Node:getValue()

Returns the value of this node.