|
File I/O object used for creating, reading, and writing XML documents. More...
Public Member Functions | |
void | addComment (string comment) |
Add the given comment as a child of the document. | |
void | addData (string text) |
Add the given text as a child of current Element. | |
void | addHeader () |
Add a XML header to a document. | |
void | addNewElement (string name) |
Create a new element with the given name as child of current Element's parent and push it onto the Element stack making it the current one. | |
void | addText (string text) |
Add the given text as a child of current Element. | |
string | attribute (string attributeName) |
Get a string attribute from the current Element on the stack. | |
bool | attributeExists (string attributeName) |
Tests if the requested attribute exists. | |
float | attributeF32 (string attributeName) |
Get float attribute from the current Element on the stack. | |
int | attributeS32 (string attributeName) |
Get int attribute from the current Element on the stack. | |
void | clear () |
Set this document to its default state. | |
void | clearError () |
Clear the last error description. | |
string | elementValue () |
Get the Element's value if it exists. | |
string | firstAttribute () |
Obtain the name of the current Element's first attribute. | |
string | getData () |
Gets the text from the current Element. | |
string | getErrorDesc () |
Get last error description. | |
string | getText () |
Gets the text from the current Element. | |
string | lastAttribute () |
Obtain the name of the current Element's last attribute. | |
bool | loadFile (string fileName) |
Load in given filename and prepare it for use. | |
string | nextAttribute () |
Get the name of the next attribute for the current Element after a call to firstAttribute(). | |
bool | nextSiblingElement (string name) |
Put the next sibling Element with the given name on the stack, making it the current one. | |
void | parse (string xmlString) |
Create a document from a XML string. | |
void | popElement () |
Pop the last Element off the stack. | |
string | prevAttribute () |
Get the name of the previous attribute for the current Element after a call to lastAttribute(). | |
bool | pushChildElement (int index) |
Push the child Element at the given index onto the stack, making it the current one. | |
bool | pushFirstChildElement (string name) |
Push the first child Element with the given name onto the stack, making it the current Element. | |
void | pushNewElement (string name) |
Create a new element with the given name as child of current Element and push it onto the Element stack making it the current one. | |
string | readComment (int index) |
Gives the comment at the specified index, if any. | |
void | removeText () |
Remove any text on the current Element. | |
void | reset () |
Set this document to its default state. | |
bool | saveFile (string fileName) |
Save document to the given file name. | |
void | setAttribute (string attributeName, string value) |
Set the attribute of the current Element on the stack to the given value. | |
void | setObjectAttributes (string objectID) |
Add the given SimObject's fields as attributes of the current Element on the stack. |
File I/O object used for creating, reading, and writing XML documents.
A SimXMLDocument is a container of various XML nodes. The Document level may contain a header (sometimes called a declaration), comments and child Elements. Elements may contain attributes, data (or text) and child Elements.
You build new Elements using addNewElement(). This makes the new Element the current one you're working with. You then use setAttribute() to add attributes to the Element. You use addData() or addText() to write to the text area of an Element.
// Thanks to Rex Hiebert for this example // Given the following XML <?xml version="1.0" encoding="utf-8" standalone="yes" ?> <DataTables> <table tableName="2DShapes"> <rec id="1">Triangle</rec> <rec id="2">Square</rec> <rec id="3">Circle</rec> </table> <table tableName="3DShapes"> <rec id="1">Pyramid</rec> <rec id="2">Cube</rec> <rec id="3">Sphere</rec> </table> </DataTables> // Using SimXMLDocument by itself function readXmlExample(%filename) { %xml = new SimXMLDocument() {}; %xml.loadFile(%filename); %xml.pushChildElement("DataTables"); %xml.pushFirstChildElement("table"); while(true) { echo("TABLE:" SPC %xml.attribute("tableName")); %xml.pushFirstChildElement("rec"); while (true) { %id = %xml.attribute("id"); %desc = %xml.getData(); echo(" Shape" SPC %id SPC %desc); if (!%xml.nextSiblingElement("rec")) break; } %xml.popElement(); if (!%xml.nextSiblingElement("table")) break; } } // Thanks to Scott Peal for this example // Using FileObject in conjunction with SimXMLDocument // This example uses an XML file with a format of: // <Models> // <Model category="" name="" path="" /> // </Models> function getModelsInCatagory() { %file = "./Catalog.xml"; %fo = new FileObject(); %text = ""; if(%fo.openForRead(%file)) { while(!%fo.isEOF()) { %text = %text @ %fo.readLine(); if (!%fo.isEOF()) %text = %text @ "\n"; } } else { echo("Unable to locate the file: " @ %file); } %fo.delete(); %xml = new SimXMLDocument() {}; %xml.parse(%text); // "Get" inside of the root element, "Models". %xml.pushChildElement(0); // "Get" into the first child element if (%xml.pushFirstChildElement("Model")) { while (true) { // // Here, i read the element's attributes. // You might want to save these values in an array or call the %xml.getElementValue() // if you have a different XML structure. %catagory = %xml.attribute("catagory"); %name = %xml.attribute("name"); %path = %xml.attribute("path"); // now, read the next "Model" if (!%xml.nextSiblingElement("Model")) break; } } }
void SimXMLDocument::addComment | ( | string | comment | ) |
Add the given comment as a child of the document.
comment | String containing the comment. |
// Create a new XML document with a header, a comment and single element. %x = new SimXMLDocument(); %x.addHeader(); %x.addComment("This is a test comment"); %x.addNewElement("NewElement"); %x.saveFile("test.xml"); // Produces the following file: // <?xml version="1.0" encoding="utf-8" standalone="yes" ?> // <!--This is a test comment--> // <NewElement />
void SimXMLDocument::addData | ( | string | text | ) |
Add the given text as a child of current Element.
Use getData() to retrieve any text from the current Element.
addData() and addText() may be used interchangeably. As there is no difference between data and text, you may also use removeText() to clear any data from the current Element.
text | String containing the text. |
// Create a new XML document with a header and single element // with some added data. %x = new SimXMLDocument(); %x.addHeader(); %x.addNewElement("NewElement"); %x.addData("Some text"); %x.saveFile("test.xml"); // Produces the following file: // <?xml version="1.0" encoding="utf-8" standalone="yes" ?> // <NewElement>Some text</NewElement>
void SimXMLDocument::addHeader | ( | ) |
Add a XML header to a document.
Sometimes called a declaration, you typically add a standard header to the document before adding any elements. SimXMLDocument always produces the following header:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
// Create a new XML document with just a header and single element. %x = new SimXMLDocument(); %x.addHeader(); %x.addNewElement("NewElement"); %x.saveFile("test.xml"); // Produces the following file: // <?xml version="1.0" encoding="utf-8" standalone="yes" ?> // <NewElement />
void SimXMLDocument::addNewElement | ( | string | name | ) |
Create a new element with the given name as child of current Element's parent and push it onto the Element stack making it the current one.
name | XML tag for the new Element. |
void SimXMLDocument::addText | ( | string | text | ) |
Add the given text as a child of current Element.
Use getText() to retrieve any text from the current Element and removeText() to clear any text.
addText() and addData() may be used interchangeably.
text | String containing the text. |
// Create a new XML document with a header and single element // with some added text. %x = new SimXMLDocument(); %x.addHeader(); %x.addNewElement("NewElement"); %x.addText("Some text"); %x.saveFile("test.xml"); // Produces the following file: // <?xml version="1.0" encoding="utf-8" standalone="yes" ?> // <NewElement>Some text</NewElement>
string SimXMLDocument::attribute | ( | string | attributeName | ) |
Get a string attribute from the current Element on the stack.
attributeName | Name of attribute to retrieve. |
bool SimXMLDocument::attributeExists | ( | string | attributeName | ) |
Tests if the requested attribute exists.
attributeName | Name of attribute being queried for. |
float SimXMLDocument::attributeF32 | ( | string | attributeName | ) |
Get float attribute from the current Element on the stack.
attributeName | Name of attribute to retrieve. |
int SimXMLDocument::attributeS32 | ( | string | attributeName | ) |
Get int attribute from the current Element on the stack.
attributeName | Name of attribute to retrieve. |
void SimXMLDocument::clear | ( | ) |
void SimXMLDocument::clearError | ( | ) |
Clear the last error description.
string SimXMLDocument::elementValue | ( | ) |
Get the Element's value if it exists.
Usually returns the text from the Element.
string SimXMLDocument::firstAttribute | ( | ) |
Obtain the name of the current Element's first attribute.
string SimXMLDocument::getData | ( | ) |
Gets the text from the current Element.
Use addData() to add text to the current Element.
getData() and getText() may be used interchangeably. As there is no difference between data and text, you may also use removeText() to clear any data from the current Element.
// Using the following test.xml file as an example: // <?xml version="1.0" encoding="utf-8" standalone="yes" ?> // <NewElement>Some data</NewElement> // Load in the file %x = new SimXMLDocument(); %x.loadFile("test.xml"); // Make the first Element the current one %x.pushFirstChildElement("NewElement"); // Store the current Element's data ('Some data' in this example) // into 'result' %result = %x.getData(); echo( %result );
string SimXMLDocument::getErrorDesc | ( | ) |
Get last error description.
string SimXMLDocument::getText | ( | ) |
Gets the text from the current Element.
Use addText() to add text to the current Element and removeText() to clear any text.
getText() and getData() may be used interchangeably.
// Using the following test.xml file as an example: // <?xml version="1.0" encoding="utf-8" standalone="yes" ?> // <NewElement>Some text</NewElement> // Load in the file %x = new SimXMLDocument(); %x.loadFile("test.xml"); // Make the first Element the current one %x.pushFirstChildElement("NewElement"); // Store the current Element's text ('Some text' in this example) // into 'result' %result = %x.getText(); echo( %result );
string SimXMLDocument::lastAttribute | ( | ) |
Obtain the name of the current Element's last attribute.
bool SimXMLDocument::loadFile | ( | string | fileName | ) |
Load in given filename and prepare it for use.
fileName | Name and path of XML document |
string SimXMLDocument::nextAttribute | ( | ) |
Get the name of the next attribute for the current Element after a call to firstAttribute().
bool SimXMLDocument::nextSiblingElement | ( | string | name | ) |
Put the next sibling Element with the given name on the stack, making it the current one.
name | String containing name of the next sibling. |
void SimXMLDocument::parse | ( | string | xmlString | ) |
Create a document from a XML string.
xmlString | Valid XML to parse and store as a document. |
void SimXMLDocument::popElement | ( | ) |
Pop the last Element off the stack.
string SimXMLDocument::prevAttribute | ( | ) |
Get the name of the previous attribute for the current Element after a call to lastAttribute().
bool SimXMLDocument::pushChildElement | ( | int | index | ) |
Push the child Element at the given index onto the stack, making it the current one.
index | Numerical index of Element being pushed. |
bool SimXMLDocument::pushFirstChildElement | ( | string | name | ) |
Push the first child Element with the given name onto the stack, making it the current Element.
name | String containing name of the child Element. |
// Using the following test.xml file as an example: // <?xml version="1.0" encoding="utf-8" standalone="yes" ?> // <NewElement>Some text</NewElement> // Load in the file %x = new SimXMLDocument(); %x.loadFile("test.xml"); // Make the first Element the current one %x.pushFirstChildElement("NewElement"); // Store the current Element's text ('Some text' in this example) // into 'result' %result = %x.getText(); echo( %result );
void SimXMLDocument::pushNewElement | ( | string | name | ) |
Create a new element with the given name as child of current Element and push it onto the Element stack making it the current one.
name | XML tag for the new Element. |
string SimXMLDocument::readComment | ( | int | index | ) |
Gives the comment at the specified index, if any.
Unlike addComment() that only works at the document level, readComment() may read comments from the document or any child Element. The current Element (or document if no Elements have been pushed to the stack) is the parent for any comments, and the provided index is the number of comments in to read back.
index | Comment index number to query from the current Element stack |
void SimXMLDocument::removeText | ( | ) |
void SimXMLDocument::reset | ( | ) |
bool SimXMLDocument::saveFile | ( | string | fileName | ) |
Save document to the given file name.
fileName | Path and name of XML file to save to. |
void SimXMLDocument::setAttribute | ( | string | attributeName, | |
string | value | |||
) |
Set the attribute of the current Element on the stack to the given value.
attributeName | Name of attribute being changed | |
value | New value to assign to the attribute |
void SimXMLDocument::setObjectAttributes | ( | string | objectID | ) |
Add the given SimObject's fields as attributes of the current Element on the stack.
objectID | ID of SimObject being copied. |