Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

E4X Adds Direct XML Support to JavaScript : Page 3

E4X provides a more intuitive, familiar, and natural way for JavaScript developers to deal with XML. E4X code is easier to read, write, and maintain than standard XML in programming models such as XSLT, XQuery, and the DOM.


advertisement
Operators
Table 3 lists E4X operators with descriptions and accompanying examples.

Operator Sign/Represented By Description Example
Delete Operator "delete" keyword This operator is for deleting XML properties and XML attributes from XML objects and XMLLists.

var xmlElement = <publishing>
<author type="freelance" books="java">Rahul Gupta</author>
</publishing>;
delete xmlElement.author.@books;
// will not show "java" attribute
for each (var xmlAttributes in xmlElement..@*)
alert("Attribute="+
xmlAttributes.toXMLString());

Typeof Operator "typeof" keyword This operator is for determining the types of XML and XMLList objects.


var xmlElement =
<author type="freelance" >Rahul Kumar Gupta</author>;
// show "xml"
alert(typeof(xmlElement));

Addition Operator + Depending on its arguments, this operator is for performing string concatenation, XML and XMLList concatenation, or numeric addition.

var xmlElement = <publishing>
<book price="100">Java</book>
<book price="150">JSP</book>
</publishing>;
totPrice= +xmlElement.book[0].@price +
+xmlElement.book[1].@price;
// show "250"
alert(totPrice);

Equality Operator == This operator is for enabling equality comparisons (including E4X objects like QName and Namespace objects, and the types XML and XMLList).

var xmlElement1 = <book
price="100">Java</book>;
var xmlElement2 = <book
price="150">Java</book>;
// shows "false"
alert( (xmlElement1 == xmlElement2));

Assignment Operator = This operator is used to modify, replace, and insert properties and XML attributes in an XML object. It has two types:
  1. XML Assignment
  2. XMLList Assignment

var xmlElement = <publishing>
<book price="100">Java</book>
<book price="150">JSP</book>
</publishing>;
xmlElement.book[1].@price = 165;
// shows "165"
alert(xmlElement.book[1].@price);

Compound Assignment op= E4X benefits from the compound assignment operator "+=". It is used for inserting the XML objects specified by the AssignmentExpression just after the XML objects specified by the LeftHandSideExpression in the context of their parent.

var xmlElement = <publishing>
<book price="100">Java</book>
<book price="150">JSP</book>
</publishing>;
xmlElement.book[0] += <book
price="200">EJB</book>;
//shows JAVA. EJB JSP
for each (var xmlElements in xmlElement.*)
alert("Element="+ xmlElements);

XML Filtering Predicate Operator xml(list)Object.(filtering predicate expression) This operator is used to apply a condition to filter out certain elements in a child node or descendent accessor.

var xmlElement= <publishing>
<author type="freelance">Rahul</author>
<book price="200">EJB</book>
<book price="150">JSP</book>
</publishing>;
//show EJB
for each (var xmlElements in xmlElement.*) {
if(xmlElements.@price == '200')
alert(xmlElements);

Table 3. E4X Operators

E4X Objects
E4X adds four native objects to ECMAScript:

  • Namespace
  • QName
  • XML
  • XMLList

In addition, E4X adds new properties to the global object. It adds native XML support to the language, meaning in addition to types like Number, String, Boolean, and Object, there is the XML type for representing XML elements, attributes, comments, processing-instructions, or text nodes, and the XMLList type for representing a list of XML objects. Both XML and XMLList are new fundamental data types for representing XML objects and lists of XML objects.

The XML type is an ordered collection of properties with a name, a set of XML attributes, a set of in-scope namespaces, and a parent. The following is an example of an XML object referring to one root node:

<rootnode> <node></node> <node></node> <node></node> </rootnode>

An XML initializer is an expression written in a form of a literal that describes the initialization of an XML object. It may specify a XML element, a XML comment, a XML PI, or a CDATA section using ordinary XML syntax. The following sample code shows one:

// an XML object representing a author with a name and age var author = <author> <name>Rahul Gupta</name> <authorType>freelance</authorType></author>; //on XML Object representing two authors var publishing = <publishing> <author type="freelance" books="java"> <name>Rahul Gupta</name> </author> <author type="employed"> <name>Jon</name> </author> </publishing>;

The XMLList type is an ordered collection of properties. A value of type XMLList represents a XML document, an XML fragment, or an arbitrary collection of XML objects. This is an example of an XMLList that refers to multiple nodes:

<node></node> <node></node> <node></node>

A XMLList initializer is an expression describing the initialization of an XMLList object. It describes an ordered list of XML properties using an anonymous XML element syntax. XMLList initializers begin with the character sequence "<>" and end with the character sequence "</>". The following snippet shows a way to initialize the XMLList object in E4X code:

var xmlIstObj = <><name>Rahul Gupta</name> <authorType>freelance</authorType></>; var authorlistObt = <> <author type="freelance" books="java"> <name>Rahul Gupta</name> </author> <author type="employed"> <name>Jon</name> </author> </>;

The main difference between the two is that with XML you are dealing with one specific object that can contain n number of child nodes while with XMLList you are dealing with a collection of one or more XML objects.

To simplify the programmer's task, E4X intentionally blurs the distinction between a single XML object and an XMLList containing only one value. To that end, E4X extends the ECMAScript's function call semantics such that all methods available for values of type XML are also available for XMLLists of size one.

Table 4 lists E4X methods and properties with descriptions and accompanying examples.

Methods/Properties Available
in XML
Available
in XML List
Description
addNamespace(..) Y   The addNamespace method adds a namespace declaration to the in-scope namespaces for an XML object and returns the XML object.
appendChild(..) Y   The appendChild method appends the given child to the end of an XML object's properties and returns the XML object.
Alternatives:
  1. Use the insertChildBefore method with the first parameter value as null.
  2. Use a compound operator.
attribute(..) Y Y The attribute method takes the names of the attributes to look for.
Alternative: xml.@attributeName OR xml.['@attributeName']
attributes(..) Y Y The attributes method returns a XMLList containing the XML attributes.
Alternative: xml.@* OR xml ['@*']
child(..) Y Y The child property takes the child property name to look for.
Alternative: xml.ChildPropertyName OR xml['ChildPropertyName']
childIndex(..) Y   The childIndex method returns a number representing the ordinal position of an XML object within the context of its parent. When xmlobject has no parent, the special number value NaN (not a number) is returned.
children(..) Y Y The children method returns a XMLList containing all the child properties.
Alternative: use wild card *
comments (..) Y Y The comments method returns an XMLList containing the properties that represent XML comments. Make sure that you have the XML.ignoreComments = false; setting before creating XMLobject.
contains(..) Y Y The contains method takes one argument: the value to check for in xmlobject/XmlList.
copy(..) Y Y The copy method returns a deep copy of this XML object/XmlList. You can make a copy of either all or part of XML object/XmlList.
descendants (..) Y Y The descendants method returns an XMLList with thedescendants matching the passed name argument or with all descendants, if no argument is passed.
Alternative: Use the descendants operator (..)
elements(..) Y Y The elements method takes on a parameter and returns an XMLList with all matching child elements or with all child elements if no argument was passed.
Alternative:

xml.childElementName xml ['childElementName']

hasOwnProperty(..) Y Y The hasOwnProperty method returns a Boolean value indicating whether the object/List has the property specified by parameter.
hasComplexContent(..) Y Y The hasComplexContent method returns a Boolean value indicating whether the XML object/XMLList contains complex content (i.e., an XML element that has child elements).
hasSimpleContent(..) Y Y The hasSimpleContent method returns a Boolean value indicating whether the XML object/list contains simple content (i.e., a text node, attribute node) or represents an XML element that has no child elements.
Note: The existence of attributes, comments, processing instructions, and text nodes within an XML object/list is not significant for determining if it has simple content.
isScopeNamespaces(..) Y   The isScopeNamespaces method returns an array of Namespace objects representing the namespaces that are in scope for this XML object.
insertChildAfter(...) Y   The insertChildAfter method takes two arguments (p1 and p2), an existing child (p1) to insert after and the new child (p2) to be inserted. If p is null, it inserts p2 before all the children of an XML object. If p1 does not exist in this XML object, it returns without modifying the XML object.
insertChildBefore(..) Y   The insertChildBefore method takes two arguments (p1 and p2), an existing child (p1) to insert before and the new child (p2) to be inserted. If p1 is null, it inserts p2 after all children in the XML object. If p1 does not exist in the XML object, it returns without modifying the XML object.
length(..) Y Y The length method returns the number of XMLObjects the XMLList contains.
Note: For XmlObject, it always returns 1.This is to make a distinction between an XML object and an XMLList object containing exactly one value.
localName(..) Y   The localName method returns the local name portion of the qualified name of the XML object.
Note: Text nodes and comment nodes do not have a name, thus for these kinds of XML objects, null is returned.
name(..) Y   The name method returns the qualified name associated with the XML object or null if the object has no name (as for text nodes and comment nodes).
namespace(..) Y   The namespace method takes one optional parameter (p1). If p1 is not specified, it returns the namespace associated with the qualified name of the XML object. Otherwise, it looks for a matching namespace in the in-scope namespace of the XML object and returns it.
namespaceDeclarations(..) Y   The namespaceDeclarations method returns an array of Namespace objects representing the namespace declarations associated with the XML object in the context of its parent.
nodeKind(..) Y   The nodeKind method returns a string representing the type of node the XMLObject is. The possible values are element, attribute, text, comment, and processing-instruction.
normalize(..) Y Y The normalize method returns XMLObject after joining adjacent text nodes and eliminating empty text nodes.
parent(..) Y Y The parent method returns the parent of this XML object, or null if there is no parent.
Note: The parent of the attribute is an element to which the attribute is attached.
processingInstructions(..) Y Y The processingInstructions method takes one parameter, the PI name to look for, and returns the XMLList with all the matching PI child nodes.
Note: If no parameter is defined, then the wild card name '*' is used to match all PI.
prependChild(..) Y   The prependChild method inserts a specified child as the first child in xmlObject.
Alternatives:
1. Use the insertChildAfter method with the first parameter value as null.
2. Use a compound operator.
propertyIsEnumerable(..) Y Y The propertyIsEnumerable method determines whether the specified property will be included in the set of properties iterated over when the XML object is used in a for-in statement. The method returns true when ToString(P) is "0"; otherwise false.
removeNamespaces(..) Y   The removeNamespaces method removes the namespace from the in-scope namespaces of the XML object.
replace(..) Y   The replace method takes two parameters (p1 and p2) and replaces the XML properties of the XML object specified by propertyName (p1) with value (p2) and returns this XML object.
Note: The propertyName parameter may be a numeric property name, an unqualified name for a set of XML elements, a qualified name for a set of XML elements, or the properties wildcard "*".
setChildren(..) Y   The setChildren method takes two parameter (p1 and p2). It replaces the XML properties (p1) of the XML object with a new set of XML properties from value (p2). Value may be a single XML object or an XMLList. SetChildren returns this XML object.
setLocalName(..) Y   The setLocalName method changes the local name of this XMLObject with the name passed in.
setName(..) Y   The setName method changes the QName of the XMLObject with the name passed in. The parameter can be a string or a QName object.
setNamespace(..) Y   The setNamespace method changes the namespace associated with the name of the XML object to the new specified namespace.
text(..) Y Y The text method returns an XMLList containing all XML properties of the XML object that represent XML text nodes.
toString(..) Y Y The toString method returns a convenient string representation of the XMLList/XmlObject.
Note: If you are looking for complete serialization of all XML objects in the XMLList, use toXMLString.
toXMLString(..) Y Y The toXMLString method returns an XML-encoded string representation of the XMLList/XMLObject. It always includes the start tag, attributes, and end tag of the XML object regardless of its content. Global settings such as XML.prettyPrinting and XML.prettyIndent format and indent the resultant string.
valueOf(..) Y Y The valueOf method simply returns the XML object/XML list it is called on.
Table 4. E4X Methods and Properties

QNames (Qualified Names) represent qualified names of XML elements and attributes. Each QName object has a local name of type string and a namespace URI of type string or null. When the namespace URI is null, the qualified name matches any namespace. A QName object can be created by using the following:

QName ( ) or QName ( Name ) or QName (Namespace, Name ) var Qnamevar = new QName('http://www.w3.org/1999/xhtml', 'author');

An XML object representing an element or attribute has an internal name property, which is a QName object having two properties, localName and uri. You can get the QName of any XMLObject by using the name(..) method. Table 5 lists QName methods and properties with their descriptions and accompanying examples.

Methods/Properties Description Example
localName The value of the localName property is a value of type string. When the value of the localName property is "*", it represents a wildcard that matches any name.

QNameVar =
QName("http://www.w3.org/1999/xhtml","author");
//shows author
alert(QNameVar.localName);

uri The value of the uri property is null or a value of type string identifying the namespace of the QName. When the value of the uri property is the empty string, this QName is said to be in no namespace.

QNameVar =
QName("http://www.w3.org/1999/xhtml","author");
//shows http://www.w3.org/1999/xhtml
alert(QNameVar.uri);

Table 5. QName Methods and Properties

Namespace objects represent XML namespaces and provide an association between a namespace prefix and a unique resource identifier (URI). The prefix is either the undefined value or a string value that may be used to reference the namespace within the lexical representation of an XML value.

You can create a Namespace object using either of the following:

Namespace ( ) or Namespace ( uriValue ) or Namespace ( prefixValue, uriValue ) var namespaceVar = new Namespace("author",'http://www.w3.org/1999/xhtml');

Table 6 lists namespace methods and properties with their descriptions and accompanying examples.

 Methods/Properties  Description Example
prefix The value of the prefix property is either the undefined value or a string value.

var xmlElement = <xhtml:p
xmlns:xhtml="http://www.w3.org/1999/xhtml">Hello how are you.</xhtml:p>;
var namespace = xmlElement.namespace();
//shows 'xhtml'
alert(xmlElement.namespace().prefix);

uri The value of the uri property is a string value. When the value of the uri property is the empty string, the Namespace represents the unnamed namespace in no namespace.

var xmlElement = <p>Hello how are you</p>;
// shows ''
alert(xmlElement.name().uri);

Table 6. Namespace Methods and Properties

When no namespace is defined for an element/XML, the uri property has as its value an empty string. Otherwise, the uri property has as its value the namespace URI. The following code shows the usage of the uri method:

var xmlElement = <p>Hello how are you</p>; // shows '' alert(xmlElement.name().uri); xmlElement = <p xmlns="http://www.w3.org/1999/xhtml"> Hello how are you.</p>; // shows 'http://www.w3.org/1999/xhtml alert(xmlElement.name().uri);



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap