Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamic XML attributes in tags?

66 views
Skip to first unread message

Erik

unread,
Feb 8, 2012, 10:36:27 AM2/8/12
to
I am attempting to write to an xml file using a file connector + xml
parser with data from a db. The xml structure needs to be something
like:

<employees>
<employee active="true">
<firstname>Erik</firstname>
<lastname>Sorensen</lastname>
</employee>
</employees>

I cannot figure out how to get the active="true" attribute to be
dynamic, based on a work attribute (i.e. active can be true or
false. I tried using the "Static Attribute Declarations" section of
the XML parser advanced section, but that does not like it when I use
an expression.

Eddie Hartman

unread,
Feb 9, 2012, 7:43:11 AM2/9/12
to
Which TDI version are you using? The SAX parser returns node
attributes (if this is enabled for the Parser). Otherwise, in TDI 7
you can use the (new) XML Parser. If you empty out the Entry Tag
parameter of the Parser then it returns a single hierarchical
attribute. You can use dot (.) notation or square-brackets to traverse
the hierarchy. So, for example if you read the above snippet in this
way, you would get a single 'employees' attribute. Then you could do
something like this:
-----
emps = work.employees.getElementsByTagName("employee")
// the above returns a NodeList
// it has only two methods: getLength() and item()

for (i = 0; i < emps.getLength(); i++) {
emp = emps.item(i)
// prepend node attribute names with @
active = emp.@active == "true"
if (!active)
continue;

entry = system.newEntry()
for (att in emp) {
entry[att.getName()] = att.getValue
}

task.dumpEntry(entry)
}
-----
As shown above, the square-bracket technique lets you use variables to
reference attribute names. It also works for sub-nodes of a
hierarchical attribute.

Hope this helps!
-Eddie

Erik

unread,
Feb 9, 2012, 9:50:05 AM2/9/12
to
> Which TDI version are you using? The SAX parser returns node
> attributes (if this is enabled for the Parser). Otherwise, in TDI 7
> you can use the (new) XML Parser. If you empty out the Entry Tag
> parameter of the Parser then it returns a single hierarchical
> attribute. You can use dot (.) notation or square-brackets to traverse
> the hierarchy. So, for example if you read the above

Thanks Eddie -- will try and play around with this. I'm using TDI
7.0, but attempting to *write* to an xml file, so no SAX parser.
0 new messages