XMLReader r = new XMLReader(); r.addHandler("node", new NodeHandler() { @Override public void process(StructuredNode node) { System.out.println(node.queryString("name")); System.out.println(node.queryValue("price").asDouble(0d)); } }); r.parse(new FileInputStream("src/examples/test.xml"));
/** * Returns a given node at the relative path. * * @param xpath the xpath used to retrieve the resulting node * @return the node returned by the given xpath expression * @throws IllegalArgumentException if an invalid xpath was given */ @Nullable public StructuredNode queryNode(String xpath) { try { Node result = (Node) compile(xpath).evaluate(node, XPathConstants.NODE); if (result == null) { return null; } return new StructuredNode(result); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }
@Override public StructuredNode getNode(String xpath) throws XPathExpressionException { return node.queryNode(xpath); }
/** * Queries a {@link sirius.kernel.commons.Value} by evaluating the given xpath. * * @param path the xpath used to retrieve property * @return a Value wrapping the value returned by the given xpath expression * @throws java.lang.IllegalArgumentException if an invalid xpath was given */ @Nonnull public Value queryValue(String path) { return Value.of(queryString(path)); }
/** * Iterates through the sub-tree and invokes the appropriate handler for each child node. * * @param nodeHandler the handler invoked for each element node * @param textNodeHandler the handler invoked for each text node */ public void visit(@Nullable Consumer<StructuredNode> nodeHandler, @Nullable Consumer<Node> textNodeHandler) { if (node.getNodeType() == Node.TEXT_NODE) { if (textNodeHandler != null) { textNodeHandler.accept(node); } } else if (node.getNodeType() == Node.ELEMENT_NODE) { if (nodeHandler != null) { nodeHandler.accept(this); } getChildren().forEach(c -> c.visit(nodeHandler, textNodeHandler)); } }
int number = part.queryValue("PartNumber").asInt(0); parts.put(number, new File(getUploadDir(uploadId), String.valueOf(number))); });
/** * Creates a new XMLStructuredInput for the given stream. * * @param in the InputStream containing the xml data. * @param close determines whether the stream should be closed after parsing or not * @throws IOException if an io error occurs while parsing the input xml */ public XMLStructuredInput(InputStream in, boolean close) throws IOException { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(in); node = StructuredNode.of(doc.getDocumentElement()); if (close) { in.close(); } } catch (ParserConfigurationException | SAXException e) { throw new IOException(e); } }
/** * Wraps the given W3C node into a structured node. * * @param node the node to wrap * @return a wrapped instance of the given node */ @Nonnull public static StructuredNode of(@Nonnull Node node) { return new StructuredNode(node); }
@Override public String toString() { return node == null ? "" : node.toString(); }
/** * Returns the value of the attribute with the given name. * * @return a {@link Value} filled with the attribute value if an attribute exists for the given name, an empty {@link Value} otherwise. */ @Nonnull public Value getAttribute(String name) { NamedNodeMap attributes = getNode().getAttributes(); if (attributes != null) { Node attribute = attributes.getNamedItem(name); if (attribute != null) { return Value.of(attribute.getNodeValue()); } } return Value.EMPTY; }
/** * Queries a string via the given XPath. All contained XML is converted to a * string. * * @param path the xpath used to retrieve the xml sub tree * @return a string representing the xml sub-tree returned by the given xpath expression * @throws IllegalArgumentException if an invalid xpath was given */ @Nullable public String queryXMLString(String path) { try { XPath xpath = XPATH.newXPath(); Object result = xpath.evaluate(path, node, XPathConstants.NODE); if (result == null) { return null; } if (result instanceof Node) { return serializeNodeAsXML((Node) result); } return result.toString().trim(); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }
/** * Returns the property at the given relative path as string. * * @param path the xpath used to retrieve property * @return a string representation of the value returned by the given xpath expression * @throws IllegalArgumentException if an invalid xpath was given */ @Nullable public String queryString(String path) { try { Object result = compile(path).evaluate(node, XPathConstants.NODE); if (result == null) { return null; } if (result instanceof Node) { String s = ((Node) result).getTextContent(); if (s != null) { return s.trim(); } return s; } return result.toString().trim(); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }
/** * Checks whether a node is not reachable or has empty content via the given * XPath. * * @param path the xpath to be checked * @return <tt>true</tt> if no node or a empty property was found, <tt>false</tt> otherwise * @throws IllegalArgumentException if an invalid xpath was given */ public boolean isEmpty(String path) { return Strings.isEmpty(queryString(path)); }
private boolean nodeUp() { if (isComplete()) { nodeHandler.process(StructuredNode.of(root)); return true; } currentNode = currentNode.getParentNode(); return false; }
/** * Returns a list of all children of this DOM node. * * @return a list containing all children of this node. If no children exist, an empty list will be returned. */ @Nonnull public List<StructuredNode> getChildren() { NodeList result = node.getChildNodes(); List<StructuredNode> resultList = new ArrayList<>(result.getLength()); for (int i = 0; i < result.getLength(); i++) { resultList.add(new StructuredNode(result.item(i))); } return resultList; }
/** * Checks whether a node or non-empty content is reachable via the given * XPath. * * @param path the xpath to be checked * @return <tt>true</tt> if a node or non empty property was found, <tt>false</tt> otherwise * @throws IllegalArgumentException if an invalid xpath was given */ public boolean isFilled(String path) { return Strings.isFilled(queryString(path)); }
/** * Returns a list of nodes at the relative path. * * @param xpath the xpath used to retrieve the resulting nodes * @return the list of nodes returned by the given xpath expression * @throws IllegalArgumentException if an invalid xpath was given */ @Nonnull public List<StructuredNode> queryNodeList(String xpath) { try { NodeList result = (NodeList) compile(xpath).evaluate(node, XPathConstants.NODESET); List<StructuredNode> resultList = new ArrayList<>(result.getLength()); for (int i = 0; i < result.getLength(); i++) { resultList.add(new StructuredNode(result.item(i))); } return resultList; } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }