Javascript XML

From Yate Documentation
(Difference between revisions)
Jump to: navigation, search
(Methods)
(Methods)
Line 75: Line 75:
 
  t = x.getTag(); // "some_ns:a"
 
  t = x.getTag(); // "some_ns:a"
  
* '''getAttribute()'''
+
* '''getAttribute(name)'''
 +
Retrieve XML attribute value.<br>
 +
Parameters:<br>
 +
'''name''': Attribute name<br>
 +
Return:<br>
 +
Requested attribute's value, '''null''' if not found.<br>
  
* '''setAttribute()'''
+
* '''setAttribute(name [, value])'''
 +
Set XML attribute.<br>
 +
Parameters:<br>
 +
'''name''': Attribute name. If value is not given '''name''' may be an object whose fields will be set as XML attributtes<br>
 +
'''value''': Attribute value. Remove the attribute if value is '''undefined''' or '''null''' object<br>
  
* '''removeAttribute()'''
+
x = new XML("a");
 +
// Set attributes
 +
obj = {a:"a", b:"b"};
 +
x.setAttribute(obj);
 +
 
 +
* '''removeAttribute(name)'''
 +
Remove XML attribute.<br>
 +
Parameters:<br>
 +
'''name''': Attribute to remove<br>
  
 
* '''attributes()'''
 
* '''attributes()'''
 +
Retrieve XML attributes.<br>
 +
Return:<br>
 +
Object with fields set to XML attributes. '''null''' if XML has no attribute.<br>
  
* '''addChild()'''
+
* '''addChild(name_xml_array [, text])'''
 +
Append XML child(ren).<br>
 +
Parameters:<br>
 +
'''name_xml_array''': Child to append. This may be:<br>
 +
- XML tag name (append a new XML element child)<br>
 +
- XML element object (append a copy of given element)<br>
 +
- Array of XML elements (append a copy of each XML element in array)<br>
 +
'''text''': Optional child text to set. This parameter is ignored if the first parameter is an array<br>
 +
Return:<br>
 +
If an XML element is added returns the added XML element or '''null''' on failure.<br>
 +
If an array of elements is added returns TRUE on success, FALSE on first failure.<br>
  
* '''getChild()'''
+
* '''getChild([name] [, namespace])'''
 +
Retrieve first XML child element.<br>
 +
Parameters:<br>
 +
'''name''': Optional child tag to search for (without namespace prefix). '''null''' and '''undefined''' are treated as missing<br>
 +
'''namespace''': Optional child namespace. '''null''' and '''undefined''' are treated as missing<br>
 +
Return:<br>
 +
XML element, '''null''' if not found.<br>
  
* '''getChildren()'''
+
* '''getChildren([name] [, namespace])'''
 +
Retrieve XML children.<br>
 +
Parameters:<br>
 +
'''name''': Optional child tag to search for (without namespace prefix). '''null''' and '''undefined''' are treated as missing<br>
 +
'''namespace''': Optional child namespace. '''null''' and '''undefined''' are treated as missing<br>
 +
Return:<br>
 +
Array of XML elements, '''null''' if no child found.<br>
  
 
* '''addText()'''
 
* '''addText()'''

Revision as of 11:03, 7 March 2016

Javascript XML.

Constructor

  • new XML(obj_tag_xml)

Create a new XML element.
Parameters:
obj_tag_xml: Source to create from:
- Object: Create a copy if the given object is an XML element
- Tag: Create a new element with given tag
- XML string: Create a new element from XML string (it must be a complete XML)
This constructor will return the null object on failure.

x = new XML("a"); // Create from tag
x = new XML("<a><b/></a>"); // Create from XML string
xx = new XML(x); // Create a copy of x
// Failures:
x = new XML("xmlgigi");  // Invalid tag name
x = new XML("<gigi></g>");  // Invalid xml string
x = new XML("<gigi>some_text"); // Incomplete xml string
obj = new Array;
x = new XML(obj); // obj is not an XML
  • new XML(tag, text)

Create an XML element with given tag and text.
Parameters:
tag: Element tag name. It must be a valid xml element name tag
text: Element text

This constructor generates a run time error (function call failure) if tag value is invalid.

  • new XML(obj, field_name [, take])

Create an XML from object field. The field may be an XML element or XML string.
Parameters:
obj: Object whose field to use
field_name: Field name
take: Optional (defaults to false). Take the XML from object instead of making a copy of it. This parameter is ignored if the field is an XML string

This constructor will return the null object on failure.

obj = {xml: "<a/>"};
x = new XML(obj,"xml"); // Parse the string and build an XML element
// Obtain an xml object from a message:
x = new XML(msg,"xml",true);  // Take the xml from message
x = new XML(msg,"xml",false); // Obtain a copy of the xml element

Methods

  • put(list, field_name [, addText])

Put the XML (a copy of it) in a list (usually a Message() object).
Parameters:
list: Destination object
field_name: Field name to use
addText: Optional. Add xml string also

x = new XML("a");
m = new Message("call.route");
x.put(m,"xml",true);
  • getOwner()

Retrieve XML object owner (top parent).
Return:
XML object owning this XML or null if none.

  • getParent()

Retrieve XML object parent.
Return:
XML parent object or null if none.

  • unprefixedTag()
  • getTag()

Retrieve XML tag with or without namespace prefix.

x = new XML("<some_ns:a/>");
t = x.unprefixedTag(); // "a"
t = x.getTag(); // "some_ns:a"
  • getAttribute(name)

Retrieve XML attribute value.
Parameters:
name: Attribute name
Return:
Requested attribute's value, null if not found.

  • setAttribute(name [, value])

Set XML attribute.
Parameters:
name: Attribute name. If value is not given name may be an object whose fields will be set as XML attributtes
value: Attribute value. Remove the attribute if value is undefined or null object

x = new XML("a");
// Set attributes
obj = {a:"a", b:"b"};
x.setAttribute(obj);
  • removeAttribute(name)

Remove XML attribute.
Parameters:
name: Attribute to remove

  • attributes()

Retrieve XML attributes.
Return:
Object with fields set to XML attributes. null if XML has no attribute.

  • addChild(name_xml_array [, text])

Append XML child(ren).
Parameters:
name_xml_array: Child to append. This may be:
- XML tag name (append a new XML element child)
- XML element object (append a copy of given element)
- Array of XML elements (append a copy of each XML element in array)
text: Optional child text to set. This parameter is ignored if the first parameter is an array
Return:
If an XML element is added returns the added XML element or null on failure.
If an array of elements is added returns TRUE on success, FALSE on first failure.

  • getChild([name] [, namespace])

Retrieve first XML child element.
Parameters:
name: Optional child tag to search for (without namespace prefix). null and undefined are treated as missing
namespace: Optional child namespace. null and undefined are treated as missing
Return:
XML element, null if not found.

  • getChildren([name] [, namespace])

Retrieve XML children.
Parameters:
name: Optional child tag to search for (without namespace prefix). null and undefined are treated as missing
namespace: Optional child namespace. null and undefined are treated as missing
Return:
Array of XML elements, null if no child found.

  • addText()
  • getText()
  • setText()
  • getChildText()
  • xmlText()
Personal tools
Namespaces

Variants
Actions
Preface
Configuration
Administrators
Developers