commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steve Pannier" <Steve_Pannier....@jacada.com>
Subject [JXPath] Question with 'name' attribute in Map element access
Date Wed, 09 Apr 2003 15:07:50 GMT
I'm trying to get a better understanding of how the
"name" attribute works in Map element access.
Let's say I have the following Map:

      {stuff=[{fruit=apple, color=Red},
              {fruit=apple, color=Green},
              {fruit=orange, color=Orange},
              {fruit=banana, color=Yellow},
              {fruit=grape, color=Green}]}

(The test program showing this is below.)

The expression "/stuff/fruit" results in:

     apple;    Type: java.lang.String
      apple;   Type: java.lang.String
      orange;  Type: java.lang.String
      banana;  Type: java.lang.String
      grape;   Type: java.lang.String

While the expression "/stuff[@name='fruit']" results in:

     apple;    Type: java.lang.String

I was expecting to see all "fruits" returned with the second
expression.  Am I wrong to expect this?



The following is my test program:

import java.util.*;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;

public class TestMap8 {

    public TestMap8(String args[]) {
    }

    private void run() {
        MapHolder mh1 = new MapHolder();
        JXPathContext context = JXPathContext.newContext(mh1.getDataMap());

        getXPathValue(context, "/stuff/fruit");
        getXPathValue(context, "/stuff[@name='fruit']");

        System.out.println("\n MainMap: " + mh1.getDataMap());
    }

    /**
     * Evaluate the given expression within the JXPath context and
     * display the results.
     */
    private static Object getXPathValue(JXPathContext context, String expr)
{
        java.util.Iterator iter;
        Object result = null;

        System.out.println("\n   ****** Result of \"" + expr + "\":");
        iter = (java.util.Iterator)context.iterate(expr);
        if (!iter.hasNext())
            System.out.println("        EMPTY!!!");
        else {
            while (iter.hasNext()) {
                result = iter.next();
                System.out.println("        " + result + ";\tType: " +
result.getClass().getName());
            }
        }
        return result;
    }

    /**
     *
     */
    public static void main(String args[]) {
        TestMap8 testMap = new TestMap8(args);
        testMap.run();
    }


public class MapHolder {
    private Map dataMap = null;

    public MapHolder() {
        // Populate map contents
        dataMap = new HashMap();
        List l0 = new ArrayList();
        dataMap.put("stuff", l0);

        Map m = new HashMap();
        m.put("fruit", "apple");
        m.put("color", "Red");
        l0.add(m);

        m = new HashMap();
        m.put("fruit", "apple");
        m.put("color", "Green");
        l0.add(m);

        m = new HashMap();
        m.put("fruit", "orange");
        m.put("color", "Orange");
        l0.add(m);

        m = new HashMap();
        m.put("fruit", "banana");
        m.put("color", "Yellow");
        l0.add(m);

        m = new HashMap();
        m.put("fruit", "grape");
        m.put("color", "Green");
        l0.add(m);
    }

    public Map getDataMap() {
        return dataMap;
    }
}
}


Regards.


Steve Pannier
Jacada, Inc.
(763) 201-0002 Ext. 219
spannier@jacada.com
http://www.jacada.com



Mime
View raw message