Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(new InputSource(new StringReader(html))); XPathExpression xpath = XPathFactory.newInstance() .newXPath().compile("//td[text()=\"Description\"]/following-sibling::td[2]"); String result = (String) xpath.evaluate(doc, XPathConstants.STRING);
public DomUtil(Document doc) { XPathFactory xpathFactory = XPathFactory.newInstance(); m_xpath = xpathFactory.newXPath(); m_document = doc; }
/** * The a "value" from an XML file using XPath. * @param xpath The XPath expression to select the value. * @param document The document from which the value is to be extracted. * @return The data value. An empty {@link String} is returned when the expression does not evaluate * to anything in the document. * @throws XPathExpressionException Invalid XPath expression. * @since 2.0 */ public static String getValue(String xpath, Document document) throws XPathExpressionException { XPath xPathProcessor = XPathFactory.newInstance().newXPath(); return xPathProcessor.compile(xpath).evaluate(document); }
private static XPathExpression createExpression(String expressionStr) { try { return XPathFactory.newInstance().newXPath().compile(expressionStr); } catch (XPathExpressionException e) { throw new AaptUtilException(e); } }
//obtain Document somehow, doesn't matter how DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); org.w3c.dom.Document doc = b.parse(new FileInputStream("page.html")); //Evaluate XPath against Document itself XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList)xPath.evaluate("/html/body/p/div[3]/a", doc.getDocumentElement(), XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); ++i) { Element e = (Element) nodes.item(i); }
String url = "http://stackoverflow.com/questions/3152138"; Document document = new Tidy().parseDOM(new URL(url).openStream(), null); XPath xpath = XPathFactory.newInstance().newXPath(); Node question = (Node) xpath.compile("//*[@id='question']//*[contains(@class,'post-text')]//p[1]").evaluate(document, XPathConstants.NODE); System.out.println("Question: " + question.getFirstChild().getNodeValue()); NodeList answerers = (NodeList) xpath.compile("//*[@id='answers']//*[contains(@class,'user-details')]//a[1]").evaluate(document, XPathConstants.NODESET); for (int i = 0; i < answerers.getLength(); i++) { System.out.println("Answerer: " + answerers.item(i).getFirstChild().getNodeValue()); }
raf.getChannel().write(xmlSamplePart); raf.close(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(f); XPathFactory xPathfactory = XPathFactory.newInstance(); NamespaceContext ctx = new NamespaceContext() { public String getNamespaceURI(String prefix) { XPath xpath = xPathfactory.newXPath(); xpath.setNamespaceContext(ctx); XPathExpression expr = xpath.compile("/ttml:tt/ttml:body/ttml:div/@smpte:backgroundImage"); NodeList nl = (NodeList) expr.evaluate(document, XPathConstants.NODESET); HashSet<String> names = new HashSet<String>(); for (int n = 0; n < nl.getLength(); n++) { names.add(nl.item(n).getNodeValue()); System.out.println(document.getFirstChild().getTextContent());
String to = assignment.getTo(); XPathFactory factory = XPathFactory.newInstance(); XPath xpathFrom = factory.newXPath(); XPathExpression exprFrom = xpathFrom.compile(from); XPath xpathTo = factory.newXPath(); XPathExpression exprTo = xpathTo.compile(to); parent = ((org.w3c.dom.Node) target).getParentNode(); targetElem = exprTo.evaluate(parent, XPathConstants.NODE); nl = (NodeList) exprFrom.evaluate(source, XPathConstants.NODESET); } else if (source instanceof String) { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.newDocument(); if (nl.getLength() == 0) { throw new RuntimeException("Nothing was selected by the from expression " + from + " on " + sourceExpr); for (int i = 0 ; i < nl.getLength(); i++) {
try { final DocumentBuilder db = XmlUtils.buildSecureDocumentBuilder(); final Document d = db.parse(stream); final XPath xpath = XPathFactory.newInstance().newXPath(); final List<NugetPackageReference> packages = new ArrayList<>(); final NodeList nodeList = (NodeList) xpath.evaluate("//PackageReference", d, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { final Node node = nodeList.item(i); final NamedNodeMap attrs = node.getAttributes(); final NugetPackageReference npr = new NugetPackageReference(); npr.setId(include.getNodeValue()); npr.setVersion(version.getNodeValue());
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); try { DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("E:/test.xml"); XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new MyNamespaceContext()); NodeList nl = (NodeList) xPath.evaluate("/ns:root/ns:author", dDoc, XPathConstants.NODESET); System.out.println(nl.getLength()); } catch (Exception e) { e.printStackTrace();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); docBuilderFactory.setValidating(true); docBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); docBuilder = docBuilderFactory.newDocumentBuilder(); docBuilder.setErrorHandler(this); xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(nsContext);
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setXIncludeAware(true); factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); final Document document = factory.newDocumentBuilder().parse(in); final XPath xPath = XPathFactory.newInstance().newXPath(); errorHandler.checkIfShouldExit(); final Node schemaNode = (Node)xPath.compile(MESSAGE_SCHEMA_XPATH_EXPR).evaluate(document, XPathConstants.NODE); final MessageSchema messageSchema = new MessageSchema(schemaNode, typeByNameMap, messageByIdMap); errorHandler.checkIfShouldExit();
Document doc = builder.parse(new InputSource(new StringReader(xmlInputString.trim()))); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); xpath.setNamespaceContext(new DefaultNamespaceContext(doc)); Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE); String textContent = result.getTextContent(); logger.log(POILogger.DEBUG, "Extracting with xpath " + xpathString + " : value is '" + textContent + "'"); XSSFCell cell = singleXmlCell.getReferencedCell(); NodeList result = (NodeList) xpath.evaluate(commonXPath, doc, XPathConstants.NODESET); int rowOffset = table.getStartCellReference().getRow() + table.getHeaderRowCount(); int columnOffset = table.getStartCellReference().getCol(); table.setDataRowCount(result.getLength()); for (int i = 0; i < result.getLength(); i++) { Node singleNode = result.item(i).cloneNode(true);
import java.io.File; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.*; import org.w3c.dom.*; public class Demo { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document document = dbf.newDocumentBuilder().parse(new File("input.xml")); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]"); Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE); b13Node.getParentNode().removeChild(b13Node); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(System.out)); } }
public XMLSanitizer(String pattern, VirtualFileFilter filter) throws Exception { this.filter = filter; XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); expression = xpath.compile(pattern); DocumentBuilderFactory DBfactory = DocumentBuilderFactory.newInstance(); DBfactory.setNamespaceAware(true); builder = DBfactory.newDocumentBuilder(); builder.setErrorHandler(null); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformer = transformerFactory.newTransformer(); }
import java.io.FileReader; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class GuestList { public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader( "tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); String guestCredit = xPath.evaluate("guest/credit", show); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - " + guestName + " (" + guestCredit + ")"); } } }
public Object evaluate(final ProcessContext context) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xpathEvaluator = factory.newXPath(); xpathEvaluator.setXPathFunctionResolver( new XPathFunctionResolver() { public XPathFunction resolveFunction(QName functionName, int arity) xpathEvaluator.setXPathVariableResolver(new XPathVariableResolver() { xpathEvaluator.setNamespaceContext(new NamespaceContext() { private static final String DROOLS_NAMESPACE_URI = "http://www.jboss.org/drools"; private String[] prefixes = {"drools", "bpmn2"}; DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return xpathEvaluator.evaluate(this.expression, builder.newDocument(), XPathConstants.BOOLEAN);
public boolean responseNotEmpty(String response) throws ExecutionException { NodeList response_body; Document doc = getDocument(response); XPath xpath = XPathFactory.newInstance().newXPath(); try { XPathExpression expr = xpath.compile("/response[@status='success']"); response_body = (NodeList)expr.evaluate(doc, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new ExecutionException(e.getCause().getMessage()); } if (response_body.getLength() > 0 && (!response_body.item(0).getTextContent().equals("") || (response_body.item(0).hasChildNodes() && response_body.item(0).getFirstChild().hasChildNodes()))) { return true; } else { return false; } }
public SpotProtocolDecoder(Protocol protocol) { super(protocol); try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); builderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); builderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); builderFactory.setXIncludeAware(false); builderFactory.setExpandEntityReferences(false); documentBuilder = builderFactory.newDocumentBuilder(); xPath = XPathFactory.newInstance().newXPath(); messageExpression = xPath.compile("//messageList/message"); } catch (ParserConfigurationException | XPathExpressionException e) { throw new RuntimeException(e); } }
public ImportProcessor(ResourceLoader loader) { this.loader = loader; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { builder = dbf.newDocumentBuilder(); XPathFactory factory=XPathFactory.newInstance(); xPath=factory.newXPath(); } catch (ParserConfigurationException e) { LOG.error("Unable to create document builder", e); throw new RuntimeException(e); } }