Stdlib.xmlToObject

Discussion of the xtools Toolkit

Moderators: Tom, Kukurykus

Mike Hale

Stdlib.xmlToObject

Post by Mike Hale »

This may not be the best way, but it makes sense to me to edit the xml as a whole. So I would not break out the global child.

Note I don't know any thing about PE6 actions so I may have edited the wrong attributes, but the concept is valid.

Code: Select all// @include '/C/Program Files/Adobe/xtools/xlib/stdlib.js'
var pXMLFile = new File('~/Desktop/PSE6.xml');
var pXML = Stdlib.readXMLFile(pXMLFile);
var ns = new Namespace ("http://ns.adobe.com/PSEContent/1.0/");
pXML.ns::global.ns::name.@value = 'newActionName';
pXML.ns::global.ns::typecategory.@value = 'newActionCategory';
Stdlib.writeXMLFile('~/Desktop/test.xml', pXML);// written to a different file for testing
var myXML = Stdlib.readXMLFile('~/Desktop/test.xml');
alert( myXML.ns::global.ns::name.@value );
xbytor

Stdlib.xmlToObject

Post by xbytor »

Mike Hale wrote:My guess is X did not intend that function to parse xml with attributes. I imagine that he has other scripts for that purpose. This function looks to me like a helper function to convert simple xml to script parameter objects similar to the one in Adobe's Image Procssor.jsx.


That sounds about right. Those pair of functions were for converting JS objects to XML and back _only_ and the usage was for complex preference files where ini files wouldn't be feasible. XML attributes are used only for metadata, specifically the underlying data types.

I know that he is really busy with a project he is working on, so in the meantime here is my fix to that function until he has time to respond.


Thanks. Dealing with xml namespaces in JS is a major bit of pain and were a bit out of scope for these functions.

I'll fold your changes into the release later this week.

-X[/url]
Mike Hale

Stdlib.xmlToObject

Post by Mike Hale »

xbytor wrote:I'll fold your changes into the release later this week.

I think that my fix needs work. If the element has both text and attributes, my fix will only convert the attributes as I replaced the obj. It work for the xml posted but I think it would fail in a lot of cases.
myranalis

Stdlib.xmlToObject

Post by myranalis »

I am getting an 'Expected ;' error when I run the above snippet in ESTK? It highlights the first :: operator. It doesn't seem to matter if the target is the ESTK or CS3...

Code: Select allpXML.ns::global.ns::name.@value = 'newActionName';
Mike Hale

Stdlib.xmlToObject

Post by Mike Hale »

It seems that CS3 is the problem. I have been running in CS4. For some reason CS3 doesn't like the ns::element access and I don't know what syntax it would accept when a namespace is required.

The CS4 guide says that if you use the global function setDefaultXMLNamespace() to set the default namespace you can then use the simple accessors( xml.element ). But that didn't work for me in CS3 with passing either a string or a namespace object to simple setDefaultXMLNamespace.
xbytor

Stdlib.xmlToObject

Post by xbytor »

For this particular case, since there is only the one global namespace, just remove it.

Code: Select allvar str = Stdlib.readfromFile(file);
str.replace('xmlns="http://ns.adobe.com/PSEContent/1.0/"', '');

var myXML = new XML(str);

This avoids all of the namespace nonsense. I'm currently using this technique in a project I'm involved with. It saved my sanity

-X