Hi,

--- BoD <[EMAIL PROTECTED]> wrote:
> 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?
There is currently no better way, but it sounds like it might make
sense to add that kind of functionality.  I'll think about it.

>    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]' !
The getPointer() method always looks for the first match. The iterate()
method is the one used when you need a collection.

>    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)
True.

> // but this one below works:
>    Iterator possibleValueLabelsIterator =
> configuratorContext.iterate(pointer+"/possible-value-labels/label");
>    while (possibleValueLabelsIterator.hasNext())
>     System.out.println(possibleValueLabelsIterator.next());
Correct.
> 
> // how come ?
I think it makes sense.  A pointer points at an individual element by
definition, so the presense of "[index]" in the path is justified.  And
once you have that [index] you are stuck with element it points to.

You might want to use the original path rather than the pointers in the
path concatenation expressions.

> 
> -eof-----------------------------------
> 
> 
> Thank you very much for your help!

> BoD
- Dmitri


__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site
http://webhosting.yahoo.com

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

Reply via email to