View Full Version : Problem with E4X and element names with periods
michelb
07-03-2009, 09:20 AM
Hi I'm need to build XML documents using Kettle and E4X however I've encountered a problem because some of the element names have periods.
e.g.
var xmlDoc=
<root>
<elem.1>blah</elem.1>
</root>
xmlDoc.root.elem.1=foo; // invalid cause of the dot
According to the E4X spec, I should be able to do
xmlDoc["elem.1"] = "foo";
but that doesn't work. Any ideas or suggestions?
MattCasters
07-03-2009, 10:39 AM
If I try this on 3.2.0GA:
var xml = <root><elem.1>foo</elem.1></root>
xml["elem.1"] = "bar";
var xmlString2 = xml.toXMLString();
then I'm seeing the element set to "bar", just as it should.
What exactly is the problem?
michelb
07-03-2009, 01:28 PM
Hi Matt,
thanks for the quick response.
My example wasn't quite right but with your info I was able to get it working (kind of).
However, I noticed another odd problem (maybe you have a quick solution for that too).
If I do this:
// begin ---
var xml = <root><parent><child><elem.1>foo</elem.1></child></parent></root>;
var xml1 = xml["elem.1"];
xml1 = "bar";
xml..child.appendChild(<elem.2>faa</elem.2>);
var xmlString2 = xml.toXMLString();
//var xml2 = xml["elem.1"];
//xml2.appendChild(<another_child_elem>boo</another_child_elem>);
// end ---
It's fine
But if I uncomment the lines and do this:
// begin ---
var xml = <root><parent><child><elem.1>foo</elem.1></child></parent></root>;
var xml1 = xml["elem.1"];
xml1 = "bar";
xml..child.appendChild(<elem.2>faa</elem.2>);
var xmlString2 = xml.toXMLString();
var xml2 = xml["elem.1"];
xml2.appendChild(<another_child_elem>boo</another_child_elem>);
// end ---
if fails with
2009/07/03 13:27:17 - Modified Java Script Value.0 - ERROR (version 3.2.0-GA, build 10572 from 2009-05-12 08.45.26 by buildguy) : Unexpected error :
2009/07/03 13:27:17 - Modified Java Script Value.0 - ERROR (version 3.2.0-GA, build 10572 from 2009-05-12 08.45.26 by buildguy) : org.pentaho.di.core.exception.KettleValueException:
2009/07/03 13:27:17 - Modified Java Script Value.0 - ERROR (version 3.2.0-GA, build 10572 from 2009-05-12 08.45.26 by buildguy) : Javascript error:
2009/07/03 13:27:17 - Modified Java Script Value.0 - ERROR (version 3.2.0-GA, build 10572 from 2009-05-12 08.45.26 by buildguy) : TypeError: appendChild is not a function, it is xml. (script#12)
Any ideas (I need to use the appendChild).
-- update --
or even (tries to append to same element ('child'))
// begin ---
var xml = <root><parent><child><elem.1>foo</elem.1></child></parent></root>;
var xml1 = xml["elem.1"];
xml1 = "bar";
xml..child.appendChild(<elem.2>faa</elem.2>);
var xmlString2 = xml.toXMLString();
var xml2 = xml["child"];
xml2.appendChild(<another_child_elem>boo</another_child_elem>);
// end ---
michelb
07-03-2009, 01:47 PM
Never mind. I figured out my problem; for some reason, I thought that with this:
var xml1 = xml["elem.1"];
xml1 = "bar";
xml1 would be a reference to xml at elem.1, not a copy ...
-- update
I think I need to review my code to make sure my variables are used correctly because this example does work:
var xml = <root><elem.1><child>foo</child></elem.1></root>
var xml_elem = xml["elem.1"];
xml_elem.appendChild = <baby>bar</baby>;
(i.e. xml_elem is a ref to xml and does add the child as expected ...)