Interface Method | Description |
get_root | Gets the document root element that encompasses all of the XML entities. |
get_fileSize | This method is not yet implemented. |
get_fileModifiedDate | This method is not yet implemented. |
get_fileUpdatedDate | This method is not yet implemented. |
get_URL | Returns the URL for the current document. |
put_URL | Loads the XML document specified in the URL. The current document is overwritten. |
get_mimeType | This method is not yet implemented. |
get_readyState | Determines the state of the parser (parsing, finished, and so on). |
get_charset | Returns a string describing the ISO character set used in the document. This is the encoding field in <?xml version="1.0" encoding="UTF-8"?>. |
put_charset | Sets the output format of the character set for the document. |
get_doctype | Returns the Document Type Definition for the document (?DOCTYPE). |
get_version | Determines the version of XML being used in the document. |
createElement | Creates a new XML element that can be added to the document tree. |
Figure 5 The IXMLElement Interface
Interface Method | Description |
get_tagName | Fetches the tag name of the element. "NAME" is the tag name of <NAME>John Doe</NAME>. |
put_tagName | Assigns the tag name of the element. |
get_parent | Retrieves the parent IXMLElement of this element object. |
setAttribute | Adds an attribute to the element. "ID" is an attribute of <CUSTOMER ID="4362">John Doe</CUSTOMER>. |
getAttribute | Retrieves an attribute's value by name. |
removeAttribute | Deletes an attribute from the element by name. |
get_children | Creates an IXMLElementCollection of the children for this element. |
get_type | Returns the type for this element, which can be ELEMENT, TEXT, COMMENT, DOCUMENT, DTD, or OTHER. |
get_text | Returns the data of element. "John Doe" is the data of <NAME>John Doe</NAME>. |
put_text | Sets the data of the element object. |
addChild | Adds an IXMLElement to this element. |
removeChild | Removes the specified IXMLElement from the child list. |
Figure 6 The IXMLElementCollection Interface
Interface Method | Description |
get_length | Determines the number of elements in the collection. |
put_length | This method has no effect and is only provided for completeness. |
get_newEnum | Allows manipulation of the children as an IEnumVARIANT. (Not available from JScript.) |
item | Retrieves an element by name or index. In the event of a list of elements matching the specified name, an IXMLElementCollection is returned instead. |
Figure 7 FillForm JScript Function
function FillForm()
{
var state = XMLDBCfg.readyState;
// Check for the parser being done
if( state == 1 || state == 2 || state == 3 )
{
// Sleep momentarily and check again
window.setTimeout("FillForm()",100);
}
else if( state == 4 )
{
// Update the form UI
var root = XMLDBCfg.root;
// Determine what the server's FTP dir is
var elem = root.children.item("FTPDIR");
// Set the text in the form
document.CfgForm.FTPDirName.value = elem.text;
// Determine what the server's inbound extension is
elem = root.children.item("INEXT");
// Set the text in the form
document.CfgForm.InFileExt.value = elem.text;
// Determine what the server's processed extension is
elem = root.children.item("PROCEXT");
// Set the text in the form
document.CfgForm.ProcFileExt.value = elem.text;
}
else
{
alert("Failed to load config data");
}
}
Figure 9 Itemlist Tag Sample
<?xhl version="1.0" encoding="utf-8"?>
<ITEMLIST>
<ITEM>
<PRODID>4</PRODID>
<NAME>Super duper bird feeder</NAME>
<PRICE>23.95</PRICE>
<QTYONHAND>1</QTYONHAND>
<COLOR>Green</COLOR>
<SHIPOPTS>0</SHIPOPTS>
</ITEM>
<ITEM>
<PRODID>5</PRODID>
<NAME>Average bird feeder</NAME>
<PRICE>3.95</PRICE>
<QTYONHAND>100</QTYONHAND>
<COLOR>White</COLOR>
<SHIPOPTS>0</SHIPOPTS>
</ITEM>
<ITEM>
<PRODID>3</PRODID>
<NAME>Thistle Bird Feed</NAME>
<PRICE>5.95</PRICE>
<QTYONHAND>1000</QTYONHAND>
<COLOR>Black</COLOR>
<SHIPOPTS>0</SHIPOPTS>
</ITEM>
</ITEMLIST>
Figure 10 Building HTML from Items
// Get the array of children items
var arrItems = root.children;
var product = null;
var length = arrItems.length;
var children = product.children;
// Loop through each <ITEM> in the <ITEMLIST> and add to
// the table
for( nIndex = 0; nIndex < length; nIndex++ )
{
// Get the <ITEM> object at this index
product = arrItems.item(nIndex);
// Now build the html code for this record
tableHTML = tableHTML + "<tr><td>" +
children.item("PRODID").text + "</td>";
tableHTML = tableHTML + "<td>" +
children.item("NAME").text + "</td>";
tableHTML = tableHTML + "<td>" +
children.item("PRICE").text + "</td>";
tableHTML = tableHTML + "<td>" +
children.item("QTYONHAND").text +"</td>";
tableHTML = tableHTML + "<td>" +
children.item("COLOR").text + "</td>";
tableHTML = tableHTML + "<td>" +
children.item("SHIPOPTS").text "</td>";
// Close this line of the table
tableHTML = tableHTML + "</tr>";
}
// Close the table tag
tableHTML = tableHTML + "</TABLE>"
Figure 11 FillForm in C++
// Get the list of child elements from the <ITEM>
IXMLElementCollection* pItemChildren = NULL;
HRESULT hr = pItem->get_children(&pItemChildren);
if( FAILED(hr) )
{
// Empty <ITEM></ITEM> pair so nothing to process
return;
}
COleVariant vtIndex((long)0,VT_I4);
// This is the IXMLElement interface for each child field
IXMLElement* pElem = NULL;
// Get the <PRODID> element
COleVariant vtElem(L"PRODID",VT_BSTR);
hr = pItemChildren->item(vtElem,vtIndex,(LPDISPATCH*)&pElem);
if( SUCCEEDED(hr) )
{
// Copy the value
// Release the element
...
}
// Get the <NAME> element
vtElem = COleVariant(L"NAME",VT_BSTR);
hr = pItemChildren->item(vtElem,vtIndex,(LPDISPATCH*)&pElem);
if( SUCCEEDED(hr) )
{
// Copy the value
// Release the element
...
}
// Get the <PRICE> element
vtElem = COleVariant(L"PRICE",VT_BSTR);
hr = pItemChildren->item(vtElem,vtIndex,(LPDISPATCH*)&pElem);
if( SUCCEEDED(hr) )
{
// Copy the value
// Release the element
...
}
// Get the <QTYONHAND> element
vtElem = COleVariant(L"QTYONHAND",VT_BSTR);
hr = pItemChildren->item(vtElem,vtIndex,(LPDISPATCH*)&pElem);
if( SUCCEEDED(hr) )
{
// Copy the value
// Release the element
...
}
// Get the <COLOR> element
vtElem = COleVariant(L"COLOR",VT_BSTR);
hr = pItemChildren->item(vtElem,vtIndex,(LPDISPATCH*)&pElem);
if( SUCCEEDED(hr) )
{
// Copy the value
// Release the element
...
}
// Get the <SHIPOPTS> element
vtElem = COleVariant(L"SHIPOPTS",VT_BSTR);
hr = pItemChildren->item(vtElem,vtIndex,(LPDISPATCH*)&pElem);
if( SUCCEEDED(hr) )
{
// Copy the value
// Release the element
...
}
// Release the children list of the <ITEM>
pItemChildren->Release();
Figure 12 ReadDB.asp
<?xhl version="1.0" encoding="utf-8"?>
<ITEMLIST>
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "dsn=XMLAPP;uid=dbo;pwd="
' Get all of the items in the database
Set ProdSet = Conn.Execute("SELECT * FROM Products")
' Loop through all of the records returning XML data for each
Do While Not ProdSet.EOF
%>
<ITEM>
<PRODID><%=ProdSet("ProdID")%></PRODID>
<NAME><%=ProdSet("Name")%></NAME>
<PRICE><%=ProdSet("Price")%></PRICE>
<QTYONHAND><%=ProdSet("QtyOnHand")%></QTYONHAND>
<COLOR><%=ProdSet("Color")%></COLOR>
<%
If ProdSet("ShipOpts") = 0 Then
%>
<SHIPOPTS>Ground</SHIPOPTS>
<%
Else
%>
<SHIPOPTS>Air</SHIPOPTS>
<%
End If
%>
</ITEM>
<%
ProdSet.MoveNext
Loop
' Clean up our open resources
Set ProdSet = Nothing
Conn.Close
Set Conn = Nothing
%>
</ITEMLIST>