Javascript E4X tutorial and examples

Poster Content
nk4um User
Posts: 2
November 2, 2006 15:10Property Accessors works
pjr, thank you very much for the solution. More importantly thanks for the E4X resource. I was googling with wrong keywords. I was able to get the E4X spec document, now. Thanks again.
nk4um Moderator
Posts: 755
November 1, 2006 22:22Ignore QName() above
I was playing around and that got left in the example!
nk4um Moderator
Posts: 755
November 1, 2006 22:21Property Accessors
The answer seems to be in section ''11.2.1 Property Accessors'' of the E4X spec.  This works...


importPackage(Packages.java.lang);

doc = <doc>
<node>
<node.1> hello </node.1>
<node.1> world </node.1>
<node.1> welcome to </node.1>
<node.1> E4X </node.1>
<node.2> NOT SEEN </node.2>
</node>
</doc>;

qn=QName("node.1");

for each ( text in doc..*["node.1"] )
{
System.out.println(text);
}


Maybe there''s an elegant escaping syntax but I haven''t found it yet!

Cheers

Pete
nk4um User
Posts: 2
November 1, 2006 20:01How do I iterate over nodes that have period in their names?
I have an xml document as follows
<doc>
<node>
<node.1> hello </node.1>
<node.1> world </node.1>
<node.1> welcome to </node.1>
<node.1> E4X </node.1>
</node>
</doc>

How do I iterate over node.1?

I would like to something like
for each ( text in doc..node.1 )
{
print (text);
}


Right now, I can''t do this because of period in the node name. Rhino interpreter is throwing an error. ''missing ) after for-loop control''

Can someone help me? Thanks.
nk4um Moderator
Posts: 485
July 29, 2005 13:47a few checks...
First things first, are you using NetKernel version 2.0.6 as E4X support is new feature? Secondly try running the E4X example in the Developer Tools -> Script Playpen - does that work?

If you get this far then it is likely a configuration issue within your development module.

What is you setup? (OS/JVM version/ How are you using E4X - in a custom module/ in workbench)

Cheers,
Tony
nk4um User
Posts: 1
July 28, 2005 16:49I have setup problems.
The information you gave was very valuable. But i think i need more help. I have set up problems. Can you go over what is need to start with rhino. I cant write js including XML data type. The error is "XML" is not defined. What could be the problem...
nk4um Moderator
Posts: 485
May 13, 2005 16:14Javascript E4X tutorial and examples
I''ve been playing with E4X within the NetKernel scripting framework because I wanted to see if the tight language binding between Javascript and XML really made writing scripts to process XML easier and more compact.

The conclusion: E4X really works well despite a few rough edges.

Getting into E4X, though, was hard because there are not many good sources of example code out there to use.

The specification is not that helpful to actually work out how to use it:
http://www.ecma-international.org/publications/standards/Ecma-357.htm

One page this page of E4X tests which was very helpful:
http://bclary.com/2004/10/03/js-tests/menubody.html

Here is quick cookbook of some code snippets I''ve discovered:
Cookbook

create a literal document:
doc = <a><b c="1"/><b c="2"/></a>;


create cursor into document
b0 = doc.b[0];


add/assign an attribute
doc.@c="new value";


delete a node:
delete doc.@c;


iterate over nodes:
for each (b in doc.b) { x = b.@c; }


find all decendants:
allc = doc..@c;


insert a new fragment:
frag = <b c="3"/>;
doc.insertChildAfter(doc.b[1], frag);


insert a new fragment at beginning:
frag = <b c="0"/>;
doc.insertChildAfter(null, frag);


parameterized locate:
cValue=2;
bNode = doc.b.(@c==cValue);


parameterized text in literal:
text="text here";
frag = <b c="4">{text}</b>;


parameterized attribute in literal:
text="4";
frag = <b/>;
frag.@c=text;


count matches:
count = doc.b.length();


NetKernel specific

Source an external resource as an XML object:
org=new XML( context.sourceAspect( "http://www.1060.org/", IAspectXmlObject).getXmlObject() );


Create a response from an XML object:
aspect=new XmlObjectAspect(doc.getXmlObject());
response=context.createResponseFrom(aspect);
response.setMimeType("text/xml");
context.setResponse(response);


Process an XML object with an XSLT transform:
req = context.createSubRequest();
req.setURI("active:xslt");
req.addArgument("operator","style.xsl");
req.addArgument("operand", new XmlObjectAspect(doc.getXmlObject()));
req.setAspectClass(IAspectXmlObject);
table=new XML(context.issueSubRequestForAspect(req).getXmlObject());



Strange things I''ve noticed

Building an XML object from an XmlObject when there is a leading comment, for example with this doc:

<!-- comment -->
<root />


You end up with a doc just containing a comment and no root element if you:
doc=new XML( context.sourceAspect( "doc.xml",IAspectXmlObject). getXmlObject() );


Building an XML object from a String when there are an comments - all comments get lost!