Hi! I'm trying to use jxpath to easily parse a simple xml file and avoid
using the DOM interfaces.
But I'm confused about one thing.
Here is the file I want to analyse:
-xml file--------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<configurator>
<field id="test1" type="TextField">
<ldap-key>test1 ldapkey</ldap-key>
<label>test1:</label>
<tooltip>test1 tooltip</tooltip>
<icon>test1 tooltip</icon>
<ldap-access>
<read/>
<write/>
</ldap-access>
<emulation>
<activex/>
<java/>
</emulation>
<possible-value-labels>
<label>possible value label 1</label>
<label>possible value label 2</label>
<label>possible value label 3</label>
</possible-value-labels>
<possible-values>
<value>toto</value>
<value>tutu</value>
<value>titi</value>
</possible-values>
<default-values>
<value>tutu</value>
</default-values>
</field>
<field id="test2" type="CheckBox">
<ldap-key>test2 ldapkey</ldap-key>
<label>test2</label>
<tooltip>test2 tooltip</tooltip>
<!--icon>test1 tooltip</icon--> <!-- no icon -->
<ldap-access>
<write/>
</ldap-access>
<emulation>
<java/>
</emulation>
<!--possible-value-labels/--> <!-- not used in a checkbox -->
<possible-values>
<value>toto</value>
<value>tutu</value>
</possible-values>
<default-values>
<value>tutu</value>
</default-values>
</field>
</configurator>
-eof-----------------------------------
And here is my code, that include my questions in the comments:
-java file-----------------------------
// [snip xml file opening]
Element rootNode = doc.getDocumentElement(); // <configurator>
JXPathContext configuratorContext = JXPathContext.newContext(rootNode);
configuratorContext.setLenient(true); // will return null if a node is not
there, instead of throwing an exception
Iterator fieldList = configuratorContext.iteratePointers("field");
while (fieldList.hasNext())
{
Pointer pointer = (Pointer)fieldList.next();
// first question : is there a better way of doing this, e.g. creating
// a new [sub]context instead of concate pointer everywhere? If so, how
// to do this?
String fieldName = (String)configuratorContext.getValue(pointer+"/@id");
String type = (String)configuratorContext.getValue(pointer+"/@type");
String LDAPKey =
(String)configuratorContext.getValue(pointer+"/ldap-key");
String label = (String)configuratorContext.getValue(pointer+"/label");
String toolTip =
(String)configuratorContext.getValue(pointer+"/tooltip"); // can be null
String icon = (String)configuratorContext.getValue(pointer+"/icon"); //
can be null
int LDAPAccess = 0;
if
(((NodePointer)configuratorContext.getPointer(pointer+"/ldap-access/read")).
isActual())
LDAPAccess += Field.LDAP_ACCESS_READ;
if
(((NodePointer)configuratorContext.getPointer(pointer+"/ldap-access/write"))
.isActual())
LDAPAccess += Field.LDAP_ACCESS_WRITE;
int emulation = 0;
if
(((NodePointer)configuratorContext.getPointer(pointer+"/emulation/activex"))
.isActual())
emulation += Field.EMULATION_ACTIVEX;
if
(((NodePointer)configuratorContext.getPointer(pointer+"/emulation/java")).is
Actual())
emulation += Field.EMULATION_JAVA;
Pointer labelsPointer = =
configuratorContext.getPointer(pointer+"/possible-value-labels/label");
System.out.println(labelsPointer);
// this prints:
// /field[1]/possible-value-labels[1]/label[1]
//
// Why the last '[1]', it's supposed to be a collection so no '[1]' !
Iterator possibleValueLabelsIterator =
configuratorContext.iterate(labelsPointer.asPath());
while (possibleValueLabelsIterator.hasNext())
System.out.println(possibleValueLabelsIterator.next());
// so this one above doesn't work (returns an Iterator with only one
element)
// but this one below works:
Iterator possibleValueLabelsIterator =
configuratorContext.iterate(pointer+"/possible-value-labels/label");
while (possibleValueLabelsIterator.hasNext())
System.out.println(possibleValueLabelsIterator.next());
// how come ?
-eof-----------------------------------
Thank you very much for your help!
BoD
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>