PBDOM_ELEMENT:
SetAttribute Syntax 2 method
Description
Adds a PBDOM_ATTRIBUTE object and its value to a
PBDOM_ELEMENT object. Any existing attribute with the same
name and namespace URI is overwritten.
Syntax
1 |
<span>pbdom_element_name.</span>SetAttribute(string <span>strName</span>, string <span>strValue</span>) |
Argument |
Description |
---|---|
pbdom_element_name |
The name of a PBDOM_ELEMENT |
strName |
The name of the PBDOM_ATTRIBUTE |
strValue |
The value of the PBDOM_ATTRIBUTE |
Return Values
PBDOM_ELEMENT. The PBDOM_ELEMENT object
modified to contain the specified PBDOM_ATTRIBUTE with
the specified value.
Throws
EXCEPTION_INVALID_ARGUMENT – One
or both of the input strings are invalid. This can happen if either
or both strings have not been initialized properly or are null.
EXCEPTION_MEMORY_ALLOCATION_FAILURE – Insufficient
memory was encountered while executing this method.
EXCEPTION_PBDOM_OBJECT_INVALID_FOR_USE – This PBDOM_ELEMENT
object’s internal implementation is null.
The occurrence of this exception is rare but can take place if severe
memory corruption occurs.
EXCEPTION_INVALID_NAME – An
invalid name for the attribute is supplied.
EXCEPTION_INVALID_STRING – An
invalid string for the attribute value is supplied.
Examples
The SetAttribute method is invoked
for the following XML element:
1 |
<code0789725045</code |
The SetAttribute method is invoked from
the following PowerScript statement, where elem_code represents
the code element:
1 |
elem_code.SetAttribute("type", "ISBN") |
The following XML element results:
1 |
<code type="ISBN">0789725045</code> |
The following example demonstrates the effect of
setting an attribute for a PBDOM_ELEMENT object when the
PBDOM_ELEMENT object already contains an attribute of the
same name. The example creates a PBDOM_DOCUMENT based on
the following document:
1 |
<root xmlns:pre1="http://www.pre.com"><br>   <child1 pre1:a="123" b="456"/><br></root> |
The child1 element already contains an
attribute named b with value 456. Calling
the SetAttribute method with name b and
value 789 creates a new attribute for child1 that
replaces the original b attribute.
1 |
PBDOM_BUILDER     pbdom_buildr<br>PBDOM_DOCUMENT   pbdom_doc<br>string strXML = "<root xmlns:pre1=~"http://www.pre.com~" ><child1 pre1:a=~"123~" b=~"456~"/></root>"<br> <br>try<br>  pbdom_buildr = Create PBDOM_BUILDER<br>  pbdom_doc = pbdom_buildr.BuildFromString (strXML)<br>  pbdom_doc.GetRootElement(). &<br>    GetChildElement("child1").SetAttribute("b", "789")<br>catch (PBDOM_EXCEPTION except)<br>  MessageBox ("PBDOM_EXCEPTION", except.GetMessage())<br>end try |
After the PBDOM_DOCUMENT object is saved and converted
to XML, the XML document looks like the following:
1 |
<root xmlns:pre1="http://www.pre.com"><br>   <child1 pre1:a="123" b="789"/><br></root> |
Usage
This method allows the caller to add an attribute/value
pair to a PBDOM_ELEMENT object. If the PBDOM_ELEMENT
object already contains an existing attribute that has the same
name as the input name and that belongs to no namespace, the original
attribute is removed from this PBDOM_ELEMENT object and
a new one (corresponding to the specified attribute name and value)
is created and set in its place.
If a PBDOM_ATTRIBUTE has been created to represent
the original attribute, it is still valid, but the attribute that
it represents has been detached from the original owner element.
Calling GetOwnerElementObject on this PBDOM_ATTRIBUTE
returns a null value.