XmlWriter
Overview
Oftentimes you want to write extracted data directly to an XML file. This class facilitates doing that. Before working with the methods below, you may wish to read our documentation about writing extracted data to XML, which contains examples of scripts that utilize these methods.
This feature is only available to Enterprise editions of screen-scraper.
XmlWriter
XmlWriter XmlWriter ( String fileName, String rootElementName ) (enterprise edition only)
XmlWriter XmlWriter ( String fileName, String rootElementName, String rootElementText ) (enterprise edition only)
XmlWriter XmlWriter ( String fileName, String rootElementName, String rootElementText, Hashtable attributes ) (enterprise edition only)
XmlWriter XmlWriter ( String fileName, String rootElementName, String rootElementText, Hashtable attributes, String characterSet ) (enterprise edition only)
Description
Initiate a XmlWriter object.
Parameters
- fileName The file path where the file will be created, as a string.
- rootElementName The root element's name in the XML file, as a string.
- rootElementText (optional) Any text to be added inside of the root node, as a string.
- attributes (optional) Hashtable of attribute names and their associated values, for the root node.
Return Values
Returns a XmlWriter. If an error is experienced it will be thrown.
Change Log
Version |
Description |
4.5 |
Available for enterprise edition. |
5.5.3a |
Added the constructor that takes a character set. |
Class Location
com.screenscraper.xml.XmlWriter
Examples
Create an XmlWriter
// Import package
import com.screenscraper.xml.*;
// Create XmlWriter
xmlWriter = new XmlWriter( "C:/students.xml", "students" );
addElement
Element XmlWriter.addElement ( String name ) (enterprise edition only)
Element XmlWriter.addElement ( String name, String text ) (enterprise edition only)
Element XmlWriter.addElement ( String name, String text, Hashtable attributes ) (enterprise edition only)
Element XmlWriter.addElement ( Element elementToAppendTo, String name ) (enterprise edition only)
Element XmlWriter.addElement ( Element elementToAppendTo, String name, String text ) (enterprise edition only)
Element XmlWriter.addElement ( Element elementToAppendTo, String name, String text, Hashtable attributes ) (enterprise edition only)
Description
Add a node to the XML file.
Parameters
- elementToAppendTo (optional) The XmlElement to which the node is being appended.
- name The element's name, as a string.
- text (optional) Any text to be added inside of the node, as a string.
- attributes (optional) Hashtable of attribute names and their associated values, for the node.
Return Values
Returns the added element object.
Change Log
Version |
Description |
4.5 |
Available for enterprise edition. |
Examples
Add Nodes to XML File
// Import package
import com.screenscraper.xml.*;
// Create XmlWriter
xmlWriter = new XmlWriter( "C:/students.xml", "students" );
// Add Student Node
student = xmlWriter.addElement( "student" );
// Add Name Node Under the Student
address = xmlWriter.addElement( student, "name", "John Smith" );
// Close XmlWriter
xmlWriter.close();
See Also
- addElements() [XmlWriter] - Add multiple nodes under a single node
addElements
Element XmlWriter.addElements ( Element elementToAppendTo, String name, Hashtable subElements ) (enterprise edition only)
Element XmlWriter.addElements ( Element elementToAppendTo, String name, String text, Hashtable subElements ) (enterprise edition only)
Element XmlWriter.addElements ( Element elementToAppendTo, String name, String text, Hashtable attributes, Hashtable subElements ) (enterprise edition only)
Element XmlWriter.addElements ( String name, Hashtable subElements ) (enterprise edition only)
Element XmlWriter.addElements ( String name, String text, Hashtable subElements ) (enterprise edition only)
Element XmlWriter.addElements ( String name, String text, Hashtable attributes, Hashtable subElements ) (enterprise edition only)
void XmlWriter.addElements ( String containingTagName, DataSet dataSet ) (enterprise edition only)
void XmlWriter.addElements ( String containingTagName, String containingTagText, DataSet dataSet ) (enterprise edition only)
void XmlWriter.addElements ( String containingTagName, String containingTagText, Hashtable attributes, DataSet dataSet ) (enterprise edition only)
Description
Add multiple nodes under a single node (new or already in existence).
Parameters
- elementToAppendTo The XmlElement to which the node is being appended.
- name The element's name, as a string.
- text (optional--pass in null to omit) Any text to be added inside of the node, as a string.
- attributes (optional--pass in null to omit) Hashtable of attribute names and their associated values, for the node.
- subElements (optional--pass in null to omit) Hashtable children nodes with node names as keys and text as values.
OR
- name The element's name, as a string.
- text (optional--pass in null to omit) Any text to be added inside of the node, as a string.
- attributes (optional--pass in null to omit) Hashtable of attribute names and their associated values, for the node.
- subElements (optional--pass in null to omit) Hashtable children nodes with node names as keys and text as values.
OR
- containingTagName The element's name, as a string.
- containingTagText (optional--pass in null to omit) Any text to be added inside of the containing node, as a string.
- attributes (optional--pass in null to omit) Hashtable of attribute names and their associated values, for the node.
- dataSet A dataSet object.
Return Values
Returns the main added element object, if one was created. It there was not a main element that was added then it returns void.
Change Log
Version |
Description |
4.5 |
Available for enterprise edition. |
Examples
Add Nodes to XML File
// Import package
import com.screenscraper.xml.*;
import java.util.Hashtable;
// Create XmlWriter
xmlWriter = new XmlWriter( "C:/students.xml", "students" );
// Student Information
info = new Hashtable();
info.put("name", "John Smith");
info.put("phone", "555-0135");
info.put("gender", "male");
// Add Student Node
student = xmlWriter.addElements( "student", info );
// Close XmlWriter
xmlWriter.close();
See Also
close
void XmlWriter.close ( ) (enterprise edition only)
Description
Close the XmlWriter.
Parameters
This method does not receive any parameters.
Return Values
Returns void.
Change Log
Version |
Description |
4.5 |
Available for enterprise edition. |
Examples
Close XmlWriter
// Import package
import com.screenscraper.xml.*;
import java.util.Hashtable;
// Create XmlWriter
xmlWriter = new XmlWriter( "C:/students.xml", "students" );
// Student Information
info = new Hashtable();
info.put("name", "John Smith");
info.put("phone", "555-0135");
info.put("gender", "male");
// Add Student Node
student = xmlWriter.addElements( "student", info );
// Close XmlWriter
xmlWriter.close();