protected boolean stream(ElementHandler handler) { // create an xpath context from the root element // TODO: cache the context, should work just the same // JXPathIntrospector.registerDynamicClass(ElementHandlerImpl.class, // ElementHandlerPropertyHandler.class); JXPathIntrospector.registerDynamicClass(NodeImpl.class, NodePropertyHandler.class); // ElementHandler rootHandler = // ((DocumentHandler) handlers.firstElement()).getDocumentElementHandler(); Node root = ((DocumentHandler) handlers.firstElement()).getParseNode(); JXPathContext jxpContext = JXPathContextFactory.newInstance().newContext(null, root); jxpContext.setLenient(true); Iterator itr = jxpContext.iterate(xpath); while (itr.hasNext()) { Object obj = itr.next(); if (handler.getParseNode().equals(obj)) { return true; } } return false; } }
@Override public <Type> Iterator<Type> iterate(String xpath) { return context.iterate(xpath); }
@Override public <Type> Iterator<Type> iterate(String xpath) { return context.iterate(xpath); }
JXPathContext ctx = JXPathContext.newContext(list); Iterator<Object> iter = ctx.iterate(".[rateCode='R1']");
public class JXPathVisitBrowser { private JXPathContext context; public JXPathVisitBrowser(Object object) { context = JXPathContext.newContext(object); } public <T> void applyVisitor(String query, AbstractVisitor<T> visitor) { Iterator iter = context.iterate(query); while (iter.hasNext()) visitor.visit((T) iter.next()); } }
// untested JXPathContext context = JXPathContext.newContext(myMap); Iterator iter = context.iterate("FIRST_LIST/SECOND_LIST/MY_MAP/MY_PRIMITIVE"); while (iter.hasNext()) { String str = (String) iter.next(); // do something with strings }
/** execute an expath query, using this object as the root of an object graph * * @param xpath query to execute * @return iterator containing series of objects that match this query */ public final Iterator findXPathIterator(String xpath) { return this.accessJXPathContext().iterate(xpath); }
@SuppressWarnings("unchecked") public <T> Collection<T> getPath(Object root, String path) { final List<T> list = new LinkedList<T>(); final JXPathContext context = JXPathContext.newContext(root); for (final Iterator<?> i = context.iterate(path); i.hasNext();) { final T next = (T) i.next(); list.add(next); } return list; }
JXPathContext context = JXPathContext.newContext(purchase); OrderType orderDetail = (OrderType) context.getValue("orderDetail"); // equivalent to purchase.getOrderDetail() for(Iterator iter = context.iterate("/orderDetail/items"); iter.hasNext()){ Item i = (Item) iter.next(); //... } // Assumes that OrderType has a items property // List<Item> getItems()
public static Iterator search(Map<String, Object> configurationRoot, String liteXPathExpression) throws IllegalArgumentException { return JXPathContext.newContext(configurationRoot).iterate(liteXPathExpression); }
@SuppressWarnings("unchecked") public <T> T getFirst(Object root, String path) { final JXPathContext context = JXPathContext.newContext(root); for (final Iterator<?> i = context.iterate(path); i.hasNext();) { return (T) i.next(); } return null; }
public Iterator iterate(Object context){ JXPathContext jpathcontext = JXPathContext.newContext(context); return jpathcontext.iterate(literal); }
public Iterator iterate(Object context){ JXPathContext jpathcontext = JXPathContext.newContext(context); return jpathcontext.iterate(literal); }
@Override public List<IBpelObject> getElementsByXPath(String xpathToBpelElement) { JXPathContext context = JXPathContext.newContext(process); List<IBpelObject> retval = new ArrayList<IBpelObject>(); for(Iterator<?> iter = context.iterate(xpathToBpelElement); iter.hasNext();){ Object o = iter.next(); IBpelObject bo = getObjectForNativeObject(o); if(bo != null) { retval.add(bo); } else { throw new RuntimeException("No object mapping for " + o + " evaluated from " + xpathToBpelElement); } } return retval; }
private static void doJsonSorting(JSONObject doc, String x) throws XPathExpressionException, IOException, JSONException { JXPathContext ctx = JXPathContext.newContext(doc); String parentX = x.substring(0,x.lastIndexOf("/")); if ("".equals(parentX)) { parentX = "/"; } String childName = x.substring(x.lastIndexOf("/")+1); Iterator it = ctx.iterate(parentX); while (it.hasNext()) { JSONObject p = (JSONObject) it.next(); JSONArray n = p.getJSONArray(childName); List allKids = new ArrayList<>(n.length()); for (int j=0; j<n.length(); j++) { allKids.add(n.get(j)); } Collections.sort(allKids, new Comparator<Object>() { @Override public int compare(Object o1, Object o2) { return o1.toString().compareTo(o2.toString()); } }); JSONArray newArray = new JSONArray(allKids); p.put(childName,newArray); } }
/** * Gets values of the specified node as a list. Use this method for JSON * arrays. * * @param path XPath to node * @return value list, possibly empty */ public List<Object> getList(String path) { List<Object> valueList = new ArrayList<Object>(); Iterator<?> valueIterator = getJXPath().iterate(path); while (valueIterator.hasNext()) { Object value = valueIterator.next(); valueList.add(value instanceof String ? StrSubstitutor .replaceSystemProperties(value) : value); } return valueList; }
public Object get(Object object, String xpath, Class target) { JXPathContext context = JXPathContext.newContext(object); Enumeration declaredPrefixes = namespaces.getDeclaredPrefixes(); while (declaredPrefixes.hasMoreElements()) { String prefix = (String) declaredPrefixes.nextElement(); String uri = namespaces.getURI(prefix); context.registerNamespace(prefix, uri); } Iterator it = context.iterate(xpath); List results = new ArrayList<Object>(); while(it.hasNext()) { results.add(it.next()); } if (results.size()==0) { throw new IllegalArgumentException("x-path gives no results."); } else if (results.size()==1) { return results.get(0); } else { return results; } }
protected boolean stream(ElementHandler handler) { //create an xpath context from the root element // TODO: cache the context, should work just the same // JXPathIntrospector.registerDynamicClass(ElementHandlerImpl.class, // ElementHandlerPropertyHandler.class); JXPathIntrospector.registerDynamicClass( NodeImpl.class, NodePropertyHandler.class ); // ElementHandler rootHandler = // ((DocumentHandler) handlers.firstElement()).getDocumentElementHandler(); Node root = ((DocumentHandler)handlers.firstElement()).getParseNode(); JXPathContext jxpContext = JXPathContextFactory.newInstance().newContext(null,root); jxpContext.setLenient(true); Iterator itr = jxpContext.iterate(xpath); for (; itr.hasNext();) { Object obj = itr.next(); if (handler.getParseNode().equals(obj)) { return true; } } return false; }
protected boolean stream(ElementHandler handler) { //create an xpath context from the root element // TODO: cache the context, should work just the same // JXPathIntrospector.registerDynamicClass(ElementHandlerImpl.class, // ElementHandlerPropertyHandler.class); JXPathIntrospector.registerDynamicClass(NodeImpl.class, NodePropertyHandler.class); // ElementHandler rootHandler = // ((DocumentHandler) handlers.firstElement()).getDocumentElementHandler(); Node root = ((DocumentHandler) handlers.firstElement()).getParseNode(); JXPathContext jxpContext = JXPathContextFactory.newInstance().newContext(null, root); jxpContext.setLenient(true); Iterator itr = jxpContext.iterate(xpath); for (; itr.hasNext();) { Object obj = itr.next(); if (handler.getParseNode().equals(obj)) { return true; } } return false; } }
protected boolean stream(ElementHandler handler) { //create an xpath context from the root element // TODO: cache the context, should work just the same // JXPathIntrospector.registerDynamicClass(ElementHandlerImpl.class, // ElementHandlerPropertyHandler.class); JXPathIntrospector.registerDynamicClass(NodeImpl.class, NodePropertyHandler.class); // ElementHandler rootHandler = // ((DocumentHandler) handlers.firstElement()).getDocumentElementHandler(); Node root = ((DocumentHandler) handlers.firstElement()).getParseNode(); JXPathContext jxpContext = JXPathContextFactory.newInstance().newContext(null, root); jxpContext.setLenient(true); Iterator itr = jxpContext.iterate(xpath); for (; itr.hasNext();) { Object obj = itr.next(); if (handler.getParseNode().equals(obj)) { return true; } } return false; } }