Upayavira wrote:
Charles F. Munat wrote:

Simple question, but can't find the answer anywhere.

I want to load an XML config file into memory, and then access its element and attribute values from flowscript using XPath. It will be a different file for each page request (these parameters will be used to help create the response).

Essentially, I'd like to create something like this in the flowscript:

function setParams(uri) {
    cocoon.context.setAttributes("params", -config file data here-);
}

Here's the config file (example):

<?xml version="1.0" encoding="UTF-8"?>
<params>
    <color>blue</color>
    <size>XL</size>
    <quantity>5</quantity>
</params>


No time to give you a thorough answer, but what you can do is:

(a) read your config file into a DOM document
      Can't tell you this off the top of my head

function loadDocument(uri) {
var parser = null;
var source = null;
var resolver = null;
try {
parser = cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
resolver = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
source = resolver.resolveURI(uri);
var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
is.setSystemId(source.getURI());
return parser.parseDocument(is);
} finally {
if (source != null)
resolver.release(source);
cocoon.releaseComponent(parser);
cocoon.releaseComponent(resolver);
}
}


(b) Create a JXPathContext, using your DOM document:
var jx = Packages.org.apache.commons.jxpath.JXPathContext.newContext(myDomObject);
jx.setLenient(true);
(c) Use the JXPathContext to access your objects.
var color = jx.getValue("/params/color");


It isn't that hard, but if you do a lot of it, you might like to wrap this in a small Java class so that you can:

var jx = MyJXPathObject.getConfigFile("myconfigfile.xml");
var color = jx.getValue("/params/color");


Regards, Upayavira


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to