XMLElement Object
Description
An object representing an XML element.
IMPORTANT
An XMLElement object must have an opening and closing tag. An XMLElement may have attributes, a value, and/or one or more children, all of type XMLElement. If an XMLElement object has a value, its node's value must include all of its character data, including CDATA sections.
An XMLElement object can be accessed using one of the following methods or properties:
- XMLData.GetRootElement
- XMLData.SetRootElement
- XMLElement.AddChildElement
- XMLElement.AddChildElementByName
- XMLElement.NextSibling
- XMLElement.Parent
- XMLElement.RootElement
- XMLElementsColl.Item
- XMLElementsColl.ItemByName
Methods and Properties
AddAttribute | Adds a new XMLAttribute item with the specified name and value to the element. |
AddCDATASection | Adds a CDATA section to the XMLElement. |
AddChildElement | Copies the specified XMLElement object, adds it as a child element, and returns the new child object. |
AddChildElementByName | Adds a child element initialized with a tag and a value. |
AddComment | Adds a comment to the element. |
AddNamespace | Adds the specified namespace to the namespace collection used by the XMLElement object. |
Attributes | Returns the all of the attributes in the element in an attribute collection. |
CDATASections | Returns the element's CDATA sections as a collection. |
CheckAttribute | Compares the actual attribute value of the specified XML attribute (of the current XML Element) to a specified value. |
CheckValue | Compares the actual value of the XML element to a specified value. |
CheckXMLPathValue | Compares the actual value of the XML element (specified by XPath) to a specified value. |
ChildElementsByPath | Returns all of the child elements that reside in the specified path. |
Clear | Clears selected elements from the XML. |
Comments | Returns the element's comments in a collection. |
ElementName | Returns the element object's tag name. |
GetNumDescendantElemByName | Returns the quantity of the element's descendants that have the specified name. |
GetValueByXPath | Returns the value of the first child node that matches the specified XPath. |
Namespace | Returns the element's namespace. |
NextSibling | Returns the next sibling element in the XML document. |
Parent | Returns the XMLElement's parent element. |
RemoveAttribute | Removes the specified attribute from the XMLElement. |
RemoveCDATASection | Removes the specified CDATA section from the XMLElement. |
RemoveChildElement | Removes the specified ChildElement from the XMLElement. |
RemoveComment | Removes the specified comment section from the XMLElement. |
RootElement | Returns the root element of the element's document. |
SetValue | Sets a new value (character data) for the XMLElement. |
ToString | Returns the inner XML of the element. |
Value | Returns the element's value (character data, including the CData section). |
AddAttribute Method
Description
Adds a new XMLAttribute item with the specified name and value to the element.
Note: If an attribute with the specified name already exists, its value is overwritten with the specified value.
Syntax
XMLElement.AddAttribute (Name,Value)
Argument | Type | Description |
---|---|---|
Name | String | The attribute name. |
Value | String | The attribute value. |
Return Value
The following example uses the AddAttribute method to add an attribute named UFT One
with the value Great
.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set attrs = root.Attributes()
msg = "At first I had " & attrs.Count() & " attributes. "
root.AddAttribute "UFT One","Great"
Set attr = attrs.ItemByName("UFT One")
if attr is nothing then
msgbox "Failed to add attribute!!!"
end if
msg = msg + "Now I have " & attrs.Count() & " attributes. "
root.RemoveAttribute "UFT One"
msg = msg + "Again I have " & attrs.Count() & " attributes."
msgbox msg
AddCDATASection Method
Description
Adds a CDATA section to the XMLElement.
Syntax
XMLElement.AddCDATASection CDataSectionString
Argument | Type | Description |
---|---|---|
CDataSectionString | String | The CDATA section to add. |
The following example uses the AddCDATASection method to add a new CData section to the root element.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set dataSections = root.CDataSections()
msg = "I had " & dataSections.Count() & " CDataSections. "
newSection = "These characters <> are only allowed in CDATASections"
root.AddCDATASection newSection
msg = msg + "Then I had " & dataSections.Count() & " CDataSections.
"root.RemoveCDATASection newSection
msg = msg + "Now I'm back with " & dataSections.Count() & " CDataSections."
msgbox msg
AddChildElement Method
Description
Copies the specified XMLElement object, adds it as a child element, and returns the new child object.
Syntax
XMLElement.AddChildElement ChildXMLElement
Argument | Type | Description |
---|---|---|
ChildXMLElement | XMLElement | The object representing the child XML element to add. |
Return Value
The following example uses the AddChildElement method to add a copy of the root element's first child element as a new child element.
Set doc = XMLUtil.CreateXML()
doc.CreateDocument "Papa"
Set root = doc.GetRootElement()
root.AddChildElementByName "Bambino","Valuable"
Set children = root.ChildElements()
Set child = children.Item(1)
root.AddChildElement(child)
if children.Count() <> 2 then
msgbox "Failed to add child"
end if
root.RemoveChildElement child
if children.Count() <> 1 then
msgbox "Failed to remove child"
end if
AddChildElementByName Method
Adds a child element initialized with a tag and a value.
Description
Adds a child element initialized with a tag and a value.
Syntax
XMLElement.AddChildElementByName (Tag, Value)
Argument | Type | Description |
---|---|---|
Tag | String | The element tag name. |
Value | String | The element value. |
Return Value
None
The following example uses the AddChildElementByName method to add a new child element to the Papa
root element.
Set doc = XMLUtil.CreateXML()
doc.CreateDocument "Papa"
Set root = doc.GetRootElement()
root.AddChildElementByName "Bambino","Valuable"
Set children = root.ChildElements()
Set child = children.Item(1)
root.AddChildElement(child)
if children.Count() <> 2 then
msgbox "Failed to add child"
end if
root.RemoveChildElement child
if children.Count() <> 1 then
msgbox "Failed to remove child"
end if
AddChildElementByName Method
Description
Adds a comment to the element.
Syntax
XMLElement.AddComment CommentString
Argument | Type | Description |
---|---|---|
CommentString | String | The comment to add. |
The following example uses the AddComment method to add a new comment to the root element.
Set doc = xmlutil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set comments = root.Comments()
msg = "First I had " & comments.Count() & " comments."
comment = "UFT One is probably the best testing tool in the world!"
root.AddComment comment
msg = msg + "Now I have " & comments.Count() & " comments."
root.RemoveComment comment
msg = msg + "Again I have " & comments.Count() & " comments."
msgbox msg
AddChildElementByName Method
Description
Adds the specified namespace to the namespace collection used by the XMLElement object.
Syntax
XMLElement.AddNamespace Prefix, URI
Argument | Type | Description |
---|---|---|
Prefix | String | The prefix to associate with the namespace you are adding |
URI | String | The namespace to add. |
IMPORTANT
After using this method to add a namespace, you must add the namespace prefix to any XPATH expressions used in subsequent steps. If an XPATH expression does not include a prefix, the namespace URI (uniform resource identifier) will be an empty namespace (and not the default namespace).
The following example validates that the XML document has the expected number of ISBN elements. Because the XML document uses the default namespace, the AddNamespace method must be used to resolve the XPath expression in the ChildElementsByPath method call.
Set doc = XMLUtil.CreateXML()
doc.Load "<Bookstore xmlns="urn:newbooks-schema“><Book><ISBN>1234</ISBN></Book><Book><ISBN>4321</ISBN></Book><Book><ISBN>6584</ISBN></Book></Bookstore>"
doc.AddNamespace("bk", "urn:newbooks-schema");
Set children = doc.ChildElementsByPath("/bk:Bookstore/bk:Book/bk:ISBN")
numOfDescendants = doc.GetRootElement().GetNumDescendantElemByName("ISBN")
If children.Count() <> numOfDescendants then
msgbox "Problem with XML structure"
End if
Attributes Property
Description
Returns the all of the attributes in the element in an attribute collection.
Syntax
XMLElement.Attributes
Return Value
IMPORTANT
The returned item collection is a copy of the collection of the element's attributes. Therefore, any changes you make to the element's attributes after retrieving the collection (such as adding or removing an attributes) will not be included. For example, if you use the Count property on the returned item collection, the returned value will be the same before and after adding or removing CDATA sections to or from the actual XML document. To ensure that the collection is up-to-date after changes are made, use the CDATASections property again to retrieve the updated collection.
The following function uses the Attributes property to print the attributes of the root element.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set attribs = root.Attributes()
numOfAttr = attribs.Count()
i = 1
While i <= numOfAttr
Set attr = attribs.Item(i)
nameStr = attr.Name()
valStr = attr.Value()
attrStr = attrStr + nameStr+"="+chr(34)+valStr+chr(34) + " "
i = i+1
Wend
msgbox "Attributes: " + attrStr
CDATASections Property
Description
Returns the element's CDATA sections as a collection.
Syntax
XMLElement.CDATASections
Return Value
IMPORTANT
The returned item collection is a copy of the collection of the element's CDATA sections. Therefore, any changes you make to the element's CDATA section after retrieving the collection (such as adding or removing a CDATA section) will not be included. For example, if you use the Count property on the returned item collection, the returned value will be the same before and after adding or removing CDATA sections to or from the actual XML document. To ensure that the collection is up-to-date after changes are made, use the CDATASections property again to retrieve the updated collection.
The following example uses the CDATASections property to compile a collection of CDataSections to count them.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set dataSections = root.CDataSections()
msgbox "I have " & dataSections.Count() & " CDataSections. "
CheckAttribute Method
Description
Compares the actual attribute value of the specified XML attribute (of the current XML Element) to a specified value. If the values do not match, the step fails.
Syntax
XMLElement.CheckAttribute (AttributeName,Value)
Argument | Type | Description |
---|---|---|
AttributeName | String | The name of the element attribute you want to check. |
Value | Variant | The expected value of the attribute. |
Return Value
Boolean. Returns TRUE if the values match, and FALSE if the values do not match.
A TRUE return value reports a Passed step to the run results; a FALSE return value reports a Failed step to the run results.
The following example checks whether the value of the children:numberOfChilds
attribute in the root element of example.xml is 3
.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
root.CheckAttribute("children:numberOfChilds",3)
CheckValue Method
Description
Compares the actual value of the XML element to a specified value. If the values do not match, the step fails.
Syntax
XMLElement.CheckValue (Value)
Argument | Type | Description |
---|---|---|
Value | Variant | The expected value of the element. |
Return Value
Boolean. Returns TRUE if the values match, and FALSE if the values do not match.
A TRUE return value reports a Passed step to the run results; a FALSE return value reports a Failed step to the run results.
The following example checks whether the value of the root element of example.xml is MyExpectedValue
.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
root.CheckValue("MyExpectedValue")
CheckXMLPathValue Method
Description
Compares the actual value of the XML element (specified by XPath) to a specified value. If the values do not match, the step fails.
Syntax
XMLElement.CheckXPath (XPath,Value)
Argument | Type | Description |
---|---|---|
XPath | Variant | The XPath value of the element you want to check relative to the current element node. To check the current element node, use "." For more details on XPath standards, see the World Wide Web Consortium (W3C) website. |
Value | Variant | The expected value of the element. |
Return Value
Boolean. Returns TRUE if the values match, and FALSE if the values do not match.
A TRUE return value reports a Passed step to the run results; a FALSE return value reports a Failed step to the run results.
The following example checks whether the value of the element in children:childElement[2]/value:childValue
is Expected value of third child
.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
root.CheckXPath("children:childElement[2]/value:childValue","Expected value of third child")
ChildElements Property
Description
Returns a collection of XMLElement objects, representing the immediate child nodes of the element.
Syntax
XMLElement.ChildElements
Return Value
IMPORTANT
The returned element collection is a copy of the collection of the document's child elements. Therefore, any changes you make to a document's elements after retrieving the collection (such as adding or removing an element) will not be included. For example, if you use the Count property on the returned item collection, the returned value will be the same before and after adding or removing CDATA sections to or from the actual XML document. To ensure that the collection is up-to-date after changes are made, use the ChildElements property again to retrieve the updated collection.
The following example loads an XML document and then uses the ChildElements property to print the number of children of the root element named Cartman.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set children = root.ChildElements()
Setchild = children.ItemByName("Cartman")
numOfChildren = 0
While Not child Is nothing
numOfChildren = numOfChildren+1
Set child = children.ItemByName("Cartman",numOfChildren+1)
Wend
msgbox "The number of children named Cartman is " & numOfChildren
ChildElementsByPath Property
Description
Returns all of the child elements that reside in the specified path.
Syntax
XMLElement.ChildElementsByPath (XPath)
Argument | Type | Description |
---|---|---|
XPath | String | The XPath value of the element you want to check relative to the current element node. To check the current element node, use "." For more details on XPath standards, see the World Wide Web Consortium (W3C) website. Note: In QuickTest 8.2 and earlier, this method accepted only XML paths in the format |
Return Value
IMPORTANT
The returned item collection is a copy of the collection of the selected child elements. Therefore, any changes you make to these child elements after retrieving the collection (such as adding or removing an element) will not be included. For example, if you use the Count property on the returned item collection, the returned value will be the same before and after adding or removing elements to or from the actual XML document. To ensure that the collection is up-to-date after changes are made, use the ChildElementsByPath property again to retrieve the updated collection.
The following example uses the ChildElementsByPath property to validate that the document has the expected number of ISBN elements
Set doc = XMLUtil.CreateXML()
doc.Load "<Bookstore><Book><ISBN>1234</ISBN></Book><Book><ISBN>4321</ISBN></Book><Book><ISBN>6584</ISBN></Book></Bookstore>"
Set children = doc.ChildElementsByPath("/Bookstore/Book/ISBN")
Set child = doc.GetRootElement().ChildElements().Item(1
Set relatives = child.ChildElementsByPath("../Book/ISBN")
if children.Count() <> relatives.Count() then
msgbox "Problem with XML structure"
end if
numOfDescendants = doc.GetRootElement().GetNumDescendantElemByName("ISBN")
if children.Count() <> numOfDescendants then
msgbox "Problem with XML structure"
end if
Clear Method
Description
You can choose to clear all or some of the following information from the XMLElement object: attributes, values, CDATA sections, and comments. This method does not affect the element's child elements.
Syntax
XMLElement.Clear(Filter)
Argument | Type | Description |
---|---|---|
Filter | Number or pre-defined constant | Optional. The XML information to be cleared: 1 or micXMLAttributes: Clears only the attributes of the specified XML document. 2 or micXMLCDataSections: Clears only the CDATA sections of the specified XML document. 4 or micXMLValues: Clears only the values of the specified XML document. Note: If you do not use this parameter, the attributes, values, comments, and CDATA sections are all cleared. |
The following example adds an attribute, value, CDATASection, and comment to the root element, and then uses the Clear method to remove the CDATA sections from the XMLElement. The message box displays the number of comments and attributes in the document (expected value = 1
), number of CDATASections (expected value = 0
), as well as the value of the comment (expected value = Goofy is a dog
).
Set doc = XMLUtil.CreateXML()
doc.CreateDocument "Toons"
Set root = doc.GetRootElement()
root.AddAttribute "Donald","Duck"
root.SetValue "Mickey is a mouse"
root.AddComment "Goofy is a dog"
root.AddCDATASection "Bugs is a bunny"
root.Clear (micXMLCDataSections)
msgbox "The root has: " & root.Comments().Count() & " comments, " & root.Attributes().Count() & " attributes, " & root.CDATASections().Count() & " CDATASections and the value of " + root.Value()
Comments Property
Description
Returns the element's comments in a collection.
Syntax
XMLElement.Comments
Return Value
IMPORTANT
The returned item collection is a copy of the collection of document comments. Therefore, any changes you make to the document comments after retrieving the collection (such as adding or removing a comment) will not be included. For example, if you use the Count property on the returned item collection, the returned value will be the same before and after adding or removing comments to or from the actual XML document. To ensure that the collection is up-to-date after changes are made, use the Comments property again to retrieve the updated collection.
The following example uses the Comments property to compile a collection of the comments in the root element to count them.
Set doc = xmlutil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set comments = root.Comments()
msgbox "I have" & comments.Count() & " comments."
ElementName Property
Description
Returns the element object's tag name.
Syntax
XMLElement.ElementName
Return Value
String
The following function loads an XML document and then uses the ElementName property to find the quantity of children under the root that have the same name as the first child of the root.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set children = root.ChildElements()
if children.Count() <> 0 then
Set firstChild = children.Item(1)
firstChildName = firstChild.ElementName()
Set sameNameChildren = children.AllItemsByName(firstChildName)
msg = "My first child name is " + firstChildName + " and I have " + FormatNumber(sameNameChildren.Count(),0) + " of them."
End If
msgbox msg
GetNumDescendantElemByName Method
Description
Returns the quantity of the element's descendants that have the specified name.
Syntax
XMLElement.GetNumDescendantElemByName (Name)
Argument | Type | Description |
---|---|---|
Name | String | The name to find. |
Return Value
Number
The following example uses the GetNumDescendantElemByName method to validate that the document has the expected number of ISBN elements
Set doc = XMLUtil.CreateXML()
doc.Load "<Bookstore><Book><ISBN>1234</ISBN></Book><Book><ISBN>4321</ISBN></Book><Book><ISBN>6584</ISBN></Book></Bookstore>"
Set children = doc.ChildElementsByPath("/Bookstore/Book/ISBN")
Set child = doc.GetRootElement().ChildElements().Item(1)
Set relatives = child.ChildElementsByPath("../Book/ISBN")
if children.Count() <> relatives.Count() then
msgbox "Problem with XML structure"
end if
numOfDescendants = doc.GetRootElement().GetNumDescendantElemByName("ISBN")
if children.Count() <> numOfDescendants then
msgbox "Problem with XML structure"
end if
GetValueByXPath Method
Description
Returns the value of the first child node that matches the specified XPath.
Syntax
XMLElement.GetValueByXPath (XPath)
Argument | Type | Description |
---|---|---|
XPath | String | The XPath value of the element whose value you want to return. To check the current element node, use "." For more details on XPath standards, see the World Wide Web Consortium (W3C) website. |
Return Value
Number. The value of the element.
Namespace Property
Description
Returns the element's namespace.
Syntax
XMLElement.Namespace
Return Value
String
The following example uses the Namespace property to find and print the value of an element's namespace.
XmlString = "<Root xmlns:myNameSpace = ""http://mercury.com"“><myNameSpace:Child></myNameSpace:Child></Root>"
Set oXmlDoc = XMLUtil.CreateXML
oXmlDoc.Load XmlString
print oXmlDoc.ToString
print oXmlDoc.GetRootElement.ChildElements.Item(1).Namespace ' will print "http://mercury.com
NextSibling Property
Description
Returns the next sibling element in the XML document.
Syntax
XMLElement.NextSibling
Return Value
The following example uses the NextSibling property to display the name of the root element's second child (next sibling of the first child).
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement
Set children = root.ChildElements()
if not children.Count() = 0 then
Set firstChild = children.Item(1)
mag = "The first child name is " + firstChild.ElementName() + ". The parent name is " + firstChild.Parent().ElementName() + ". "
Set brother = firstChild.NextSibling()
If brother Is nothing Then
brotherStr = "I am the last of my brothers - or I have no brothers"
Else
brotherStr = "His first brother name is " + brother.ElementName() + "."
End If
End If
msgbox brotherStr
Parent Property
Description
Returns the XMLElement's parent element.
Syntax
XMLElement.Parent
Return Value
The following example uses the Parent property to display the parent name of the root element's first child.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement
Set children = root.ChildElements()
if not children.Count() = 0 then
Set firstChild = children.Item(1)
mag = "The first child name is " + firstChild.ElementName() + ". The parent name is " + firstChild.Parent().ElementName() + ". "
End If
msgbox mag
RemoveAttribute Method
Description
Removes the specified attribute from the XMLElement.
Syntax
XMLElement.RemoveAttribute Name
Argument | Type | Description |
---|---|---|
Name | String | The attribute name. |
The following example uses the RemoveAttribute method to remove the new UFT One
attribute.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set attrs = root.Attributes()
msg = "At first I had " & attrs.Count() & " attributes. "
root.AddAttribute "UFT One","Great"
Set attr = attrs.ItemByName("UFT One")
if attr is nothing then
msgbox "Failed to add attribute!!!"
end if
msg = msg + "Now I have " & attrs.Count() & " attributes. "
root.RemoveAttribute "UFT One"
msg = msg + "Again I have " & attrs.Count() & " attributes."
msgbox msg
RemoveCDATASection Method
Description
Removes the specified CDATA section from the XMLElement.
Syntax
XMLElement.RemoveCDATASection CDataSectionString
Argument | Type | Description |
---|---|---|
CDataSectionString | String | The CDATA string to remove. |
The following example uses the RemoveCDATASection method to remove the new CData section from the root element.
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set dataSections = root.CDataSections()
msg = "I had " & dataSections.Count() & " CDataSections. "
newSection = "These characters <> are only allowed in CDATASections"
root.AddCDATASection newSection
msg = msg + "Then I had " & dataSections.Count() & " CDataSections."
root.RemoveCDATASection newSection
msg = msg + "Now I'm back with " & dataSections.Count() & " CDataSections."
msgbox msg
RemoveChildElement Method
Description
Removes the specified ChildElement from the XMLElement.
Syntax
XMLElement.RemoveChildElement ChildElement
Argument | Type | Description |
---|---|---|
ChildElement | XMLElement | The name of the child element to remove. |
The following example uses the RemoveChildElement method to remove the new child
element.
Set doc = XMLUtil.CreateXML()
doc.CreateDocument "Papa"
Set root = doc.GetRootElement()
root.AddChildElementByName "Bambino","Valuable"
Set children = root.ChildElements()
Set child = children.Item(1)
root.AddChildElement(child)
if children.Count() <> 2 then
msgbox "Failed to add child"
end if
root.RemoveChildElement child
if children.Count() <> 1 then
msgbox "Failed to remove child"
end if
RemoveComment Method
Description
Removes the specified comment section from the XMLElement.
Syntax
XMLElement.RemoveComment CommentString
Argument | Type | Description |
---|---|---|
CommentString | String | The comment string to remove. |
The following example uses the RemoveComment method to remove the new comment represented by the comment
variable.
Set doc = xmlutil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement()
Set comments = root.Comments()
msg = "First I had " & comments.Count() & " comments."
comment = "UFT One is probably the best testing tool in the world!"
root.AddComment comment
msg = msg + "Now I have " & comments.Count() & " comments."
root.RemoveComment comment
msg = msg + "Again I have " & comments.Count() & " comments."
msgbox msg
RootElement Property
Description
Returns the root element of the element's document.
Syntax
XMLElement.RootElement
Return Value
The following example uses the RootElement property to retrieve the root element of the grandChild
's XML document.
Set doc = XMLUtil.CreateXML()
doc.CreateDocument "Mama"
Set root = doc.GetRootElement()
root.SetValue("Abuela")
root.AddChildElementByName "Chica",""
Set child = root.ChildElements().Item(1)
child.AddChildElementByName "Chicita",""
Set grandChild = child.ChildElements().Item(1)
if root is grandChild.RootElement() then
msgbox "I found my grandmother; her value is " + root.Value()
end if
SetValue Method
Description
Sets a new value (character data) for the XMLElement.
Note: If the element already had a value, the new value replaces it.
Syntax
XMLElement.SetValue (ValueString)
The following example uses the SetValue method to assign a new value to the Mama
root element.
Set doc = XMLUtil.CreateXML()
doc.CreateDocument "Mama"
Set root = doc.GetRootElement()
root.SetValue("Abuela")
root.AddChildElementByName "Chica",""
Set child = root.ChildElements().Item(1)
child.AddChildElementByName "Chicita",""
Set grandChild = child.ChildElements().Item(1)
if root is grandChild.RootElement() then
msgbox "I found my grandmother; her value is " + root.Value()
end if
ToString Method
Description
Returns the inner XML of the element.
Syntax
XMLElement.ToString
The following example uses the ToString method to print the contents of the XMLString object.
XmlString = "<Root xmlns:myNameSpace = ""http://mercury.com"" myNameSpace:Attr1=""Attr1"“></Root>"
Set oXmlDoc = XMLUtil.CreateXML
oXmlDoc.Load XmlString
print oXmlDoc.ToString
Value Property
Description
Returns the element's value (character data, including the CData section).
IMPORTANT
Prior to QuickTest Professional 9.0, this property did not return the CData section. You may need to update your steps to account for this change.
Syntax
XMLElement.Value
Return Value
String
The following example uses the Value property to display the value of the root element:
Set doc = XMLUtil.CreateXML()
doc.LoadFile "c:\example.xml"
Set root = doc.GetRootElement
msg = "My name is "
nameStr = root.ElementName()
valueStr = root.Value()
msgbox msg+nameStr + " and my value is " + valueStr