本文整理匯總了Java中javax.xml.xpath.XPath類的典型用法代碼示例。如果您正苦於以下問題:Java XPath類的具體用法?Java XPath怎麽用?Java XPath使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
XPath類屬於javax.xml.xpath包,在下文中一共展示了XPath類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: unmarshall
import javax.xml.xpath.XPath; //導入依賴的package包/類
@Override
public SdkServiceException unmarshall(Node in) throws Exception {
XPath xpath = xpath();
String error = asString("Error/Code", in, xpath);
String requestId = asString("Error/RequestId", in, xpath);
String message = asString("Error/Message", in, xpath);
if (!StringUtils.equals(error, this.errorCode)) {
return null;
}
SdkServiceException exception = newException(message);
exception.errorCode(errorCode);
exception.requestId(requestId);
return exception;
}
示例2: testAddRoots
import javax.xml.xpath.XPath; //導入依賴的package包/類
public void testAddRoots() throws Exception {
NbModuleProject prj = TestBase.generateStandaloneModule(getWorkDir(), "module");
FileObject src = prj.getSourceDirectory();
FileObject jar = TestFileUtils.writeZipFile(FileUtil.toFileObject(getWorkDir()), "a.jar", "entry:contents");
URL root = FileUtil.getArchiveRoot(jar.toURL());
assertTrue(ProjectClassPathModifier.addRoots(new URL[] {root}, src, ClassPath.COMPILE));
assertFalse(ProjectClassPathModifier.addRoots(new URL[] {root}, src, ClassPath.COMPILE));
FileObject releaseModulesExt = prj.getProjectDirectory().getFileObject("release/modules/ext");
assertNotNull(releaseModulesExt);
assertNotNull(releaseModulesExt.getFileObject("a.jar"));
jar = TestFileUtils.writeZipFile(releaseModulesExt, "b.jar", "entry2:contents");
root = FileUtil.getArchiveRoot(jar.toURL());
assertTrue(ProjectClassPathModifier.addRoots(new URL[] {root}, src, ClassPath.COMPILE));
assertFalse(ProjectClassPathModifier.addRoots(new URL[] {root}, src, ClassPath.COMPILE));
assertEquals(2, releaseModulesExt.getChildren().length);
String projectXml = prj.getProjectDirectory().getFileObject("nbproject/project.xml").toURL().toString();
InputSource input = new InputSource(projectXml);
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(nbmNamespaceContext());
assertEquals(projectXml, "ext/a.jar", xpath.evaluate("//nbm:class-path-extension[1]/nbm:runtime-relative-path", input));
assertEquals(projectXml, "release/modules/ext/a.jar", xpath.evaluate("//nbm:class-path-extension[1]/nbm:binary-origin", input));
assertEquals(projectXml, "ext/b.jar", xpath.evaluate("//nbm:class-path-extension[2]/nbm:runtime-relative-path", input));
assertEquals(projectXml, "release/modules/ext/b.jar", xpath.evaluate("//nbm:class-path-extension[2]/nbm:binary-origin", input));
}
示例3: unmarshall
import javax.xml.xpath.XPath; //導入依賴的package包/類
@Override
public SdkServiceException unmarshall(Node in) throws Exception {
XPath xpath = XpathUtils.xpath();
String errorCode = parseErrorCode(in, xpath);
String message = XpathUtils.asString("Response/Errors/Error/Message", in, xpath);
String requestId = XpathUtils.asString("Response/RequestID", in, xpath);
String errorType = XpathUtils.asString("Response/Errors/Error/Type", in, xpath);
SdkServiceException exception = newException(message);
exception.errorCode(errorCode);
exception.requestId(requestId);
if ("Client".equalsIgnoreCase(errorType)) {
exception.errorType(ErrorType.CLIENT);
} else if ("Server".equalsIgnoreCase(errorType)) {
exception.errorType(ErrorType.SERVICE);
} else {
exception.errorType(ErrorType.fromValue(errorType));
}
return exception;
}
示例4: vulStuurgegevens
import javax.xml.xpath.XPath; //導入依賴的package包/類
@Override
public final void vulStuurgegevens(final T verzoek, final Node node, final XPath xPath) throws ParseException {
verzoek.getStuurgegevens().setCommunicatieId(
getNodeTextContent(getPrefix() + "/brp:stuurgegevens/@brp:communicatieID", xPath, node));
verzoek.getStuurgegevens()
.setZendendePartijCode(getNodeTextContent(getPrefix() + "/brp:stuurgegevens/brp:zendendePartij", xPath, node));
verzoek.getStuurgegevens().setZendendSysteem(
getNodeTextContent(getPrefix() + "/brp:stuurgegevens/brp:zendendeSysteem", xPath, node));
verzoek.getStuurgegevens()
.setReferentieNummer(getNodeTextContent(getPrefix() + "/brp:stuurgegevens/brp:referentienummer", xPath, node));
final String tijdstipVerzendingString = getNodeTextContent(getPrefix() + "/brp:stuurgegevens/brp:tijdstipVerzending", xPath, node);
if (tijdstipVerzendingString != null) {
final ZonedDateTime tijdstipVerzending = DatumUtil.vanXsdDatumTijdNaarZonedDateTime(tijdstipVerzendingString);
verzoek.getStuurgegevens().setTijdstipVerzending(tijdstipVerzending);
}
}
示例5: unwrapSoapMessage
import javax.xml.xpath.XPath; //導入依賴的package包/類
public Element unwrapSoapMessage(Element soapElement) {
XPath xpath = XPathFactory.newInstance().newXPath();
NamespaceContextImpl context = new NamespaceContextImpl();
context.startPrefixMapping("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
context.startPrefixMapping("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
context.startPrefixMapping("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
context.startPrefixMapping("ds", "http://www.w3.org/2000/09/xmldsig#");
xpath.setNamespaceContext(context);
try {
final String expression = "/soapenv:Envelope/soapenv:Body/samlp:Response";
Element element = (Element) xpath.evaluate(expression, soapElement, XPathConstants.NODE);
if (element == null) {
String errorMessage = format("Document{0}{1}{0}does not have element {2} inside it.", NEW_LINE,
writeToString(soapElement), expression);
LOG.error(errorMessage);
throw new IllegalArgumentException(errorMessage);
}
return element;
} catch (XPathExpressionException e) {
throw propagate(e);
}
}
示例6: findElementValue
import javax.xml.xpath.XPath; //導入依賴的package包/類
/**
* Find the element reference for the selected element. The selected element
* is an XPATH expression relation to the element reference given as a
* parameter.
*
* @param pev the element reference from which the XPATH expression is
* evaluated.
* @param path the XPATH expression
* @return the resulting element reference
*/
public final ElementValue findElementValue(ElementValue pev, String path) {
XPath xpath = XPathFactory.newInstance().newXPath();
try {
Element el = (Element) xpath.evaluate(path, pev.getElement(), XPathConstants.NODE);
return pev.isInHerited()
? (el == null
? new ElementValue(Type.INHERITED_MISSING)
: new ElementValue(el, Type.INHERITED))
: (el == null
? new ElementValue(pev, path)
: new ElementValue(el, Type.OK));
} catch (XPathExpressionException ex) {
return new ElementValue(pev, path);
}
}
示例7: countColumns
import javax.xml.xpath.XPath; //導入依賴的package包/類
protected static void countColumns(XPath xPath, Table table, Node tableHead)
throws XPathExpressionException, DOMException, NumberFormatException {
if (tableHead != null) {
Node firstRow = (Node) xPath.compile("*[1]").evaluate(tableHead, XPathConstants.NODE);
NodeList childFirstRows = firstRow.getChildNodes();
int columnNumber = 0;
for (int w = 0; w < childFirstRows.getLength(); w++) {
Node childFirstRow = childFirstRows.item(w);
if (childFirstRow.getNodeValue() == null && (childFirstRow.getNodeName() == "th" || childFirstRow.getNodeName() == "td")) {
int number = 1;
if (childFirstRow.getAttributes().getNamedItem("colspan") != null) {
number = Integer.parseInt(childFirstRow.getAttributes().getNamedItem("colspan").getNodeValue());
}
columnNumber = columnNumber + number;
}
}
table.setColumnNumber(columnNumber);
}
}
示例8: eval
import javax.xml.xpath.XPath; //導入依賴的package包/類
public boolean eval() throws BuildException {
if (nullOrEmpty(fileName)) {
throw new BuildException("No file set");
}
File file = new File(fileName);
if (!file.exists() || file.isDirectory()) {
throw new BuildException(
"The specified file does not exist or is a directory");
}
if (nullOrEmpty(path)) {
throw new BuildException("No XPath expression set");
}
XPath xpath = XPathFactory.newInstance().newXPath();
InputSource inputSource = new InputSource(fileName);
Boolean result = Boolean.FALSE;
try {
result = (Boolean) xpath.evaluate(path, inputSource,
XPathConstants.BOOLEAN);
} catch (XPathExpressionException e) {
throw new BuildException("XPath expression fails", e);
}
return result.booleanValue();
}
示例9: getNodeList
import javax.xml.xpath.XPath; //導入依賴的package包/類
public static Iterator<Node> getNodeList(String exp, Element dom) throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expUserTask = xpath.compile(exp);
final NodeList nodeList = (NodeList) expUserTask.evaluate(dom, XPathConstants.NODESET);
return new Iterator<Node>() {
private int index = 0;
@Override
public Node next() {
return nodeList.item(index++);
}
@Override
public boolean hasNext() {
return (nodeList.getLength() - index) > 0;
}
};
}
示例10: performFileExistenceChecks
import javax.xml.xpath.XPath; //導入依賴的package包/類
/**
* Performs all file existence checks contained in the validation profile. If the validation has failed (some of the files specified
* in the validation profile do not exist), {@link MissingFile} exception is thrown.
*
* @param sipPath path to the SIP
* @param validationProfileDoc document with the validation profile
* @param validationProfileId id of the validation profile
* @throws XPathExpressionException if there is an error in the XPath expression
*/
private void performFileExistenceChecks(String sipPath, Document validationProfileDoc, String validationProfileId) throws
XPathExpressionException {
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xPath.compile("/profile/rule/fileExistenceCheck")
.evaluate(validationProfileDoc, XPathConstants.NODESET);
for (int i = 0; i< nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
String relativePath = element.getElementsByTagName("filePath").item(0).getTextContent();
String absolutePath = sipPath + relativePath;
if (!ValidationChecker.fileExists(absolutePath)) {
log.info("Validation of SIP with profile " + validationProfileId + " failed. File at \"" + relativePath + "\" is missing.");
throw new MissingFile(relativePath, validationProfileId);
}
}
}
示例11: performValidationSchemaChecks
import javax.xml.xpath.XPath; //導入依賴的package包/類
/**
* Performs all validation schema checks contained in the validation profile. If the validation has failed (some of the XMLs
* specified in the validation profile do not match their respective validation schemas) {@link SchemaValidationError} is thrown.
*
* @param sipPath path to the SIP
* @param validationProfileDoc document with the validation profile
* @param validationProfileId id of the validation profile
* @throws IOException if some of the XMLs address from the validation profile does not exist
* @throws XPathExpressionException if there is an error in the XPath expression
* @throws SAXException if the XSD schema is invalid
*/
private void performValidationSchemaChecks(String sipPath, Document validationProfileDoc, String validationProfileId) throws
XPathExpressionException, IOException, SAXException {
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xPath.compile("/profile/rule/validationSchemaCheck")
.evaluate(validationProfileDoc,
XPathConstants.NODESET);
for (int i = 0; i< nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
String filePath = element.getElementsByTagName("filePath").item(0).getTextContent();
String schema = element.getElementsByTagName("schema").item(0).getTextContent();
String absoluteFilePath = sipPath + filePath;
try {
ValidationChecker.validateWithXMLSchema(new FileInputStream(absoluteFilePath), new InputStream[] {
new ByteArrayInputStream(schema.getBytes(StandardCharsets.UTF_8.name()))});
} catch (GeneralException e) {
log.info("Validation of SIP with profile " + validationProfileId + " failed. File at \"" + filePath + "\" is not " +
"valid against its corresponding schema.");
throw new SchemaValidationError(filePath, schema, e.getMessage());
}
}
}
示例12: getValueFromStatus
import javax.xml.xpath.XPath; //導入依賴的package包/類
/**
* Get value from status XML doc.
*
* @param doc Doc to get value from
* @param elementName Element name to search
* @param attribute Attribute to search
* @return The value found
* @throws XPathExpressionException Error in XPath expression
*/
public static String getValueFromStatus(final Document doc,
final String elementName,
final String attribute)
throws XPathExpressionException {
// Create XPath
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
StringBuilder expression = new StringBuilder();
// Build XPath from element name and attribute value (if exists)
expression.append("//").append(elementName);
if (attribute != null) {
expression.append("[@name=\'").append(attribute).append("\']");
}
expression.append("/text()");
XPathExpression xPathExpression = xPath.compile(expression.toString());
// Return result from XPath expression
return xPathExpression.evaluate(doc);
}
示例13: findWithXPath
import javax.xml.xpath.XPath; //導入依賴的package包/類
/**
* Searches XML element with XPath and returns list of nodes found
*
* @param xml input stream with the XML in which the element is being searched
* @param expression XPath expression used in search
* @return {@link NodeList} of elements matching the XPath in the XML
* @throws XPathExpressionException if there is an error in the XPath expression
* @throws IOException if the XML at the specified path is missing
* @throws SAXException if the XML cannot be parsed
* @throws ParserConfigurationException
*/
public static NodeList findWithXPath(InputStream xml, String expression)
throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xml);
doc.getDocumentElement().normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
try {
return (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
throw new InvalidXPathException(expression, e);
}
}
示例14: getStringValue
import javax.xml.xpath.XPath; //導入依賴的package包/類
private static String getStringValue(@NonNull IAbstractFile file, @NonNull String xPath)
throws StreamException, XPathExpressionException {
XPath xpath = AndroidXPathFactory.newXPath();
InputStream is = null;
try {
is = file.getContents();
return xpath.evaluate(xPath, new InputSource(is));
} finally {
try {
Closeables.close(is, true /* swallowIOException */);
} catch (IOException e) {
// cannot happen
}
}
}
示例15: getAfnemerIndicatie
import javax.xml.xpath.XPath; //導入依賴的package包/類
private Afnemerindicatie getAfnemerIndicatie(final XPath xPath, final Node node) {
final Afnemerindicatie afnemerindicatie = new Afnemerindicatie();
afnemerindicatie
.setBsn(getNodeTextContent(getActieSpecifiekePrefix() + "/brp:persoon/brp:identificatienummers/brp:burgerservicenummer", xPath, node));
afnemerindicatie.setPartijCode(
getNodeTextContent(getActieSpecifiekePrefix() + "/brp:persoon/brp:afnemerindicaties/brp:afnemerindicatie/brp:partijCode", xPath, node));
final String datumAanvangMaterielePeriode =
getNodeTextContent(
getActieSpecifiekePrefix() + "/brp:persoon/brp:afnemerindicaties/brp:afnemerindicatie/brp:datumAanvangMaterielePeriode", xPath, node);
if (datumAanvangMaterielePeriode != null) {
afnemerindicatie
.setDatumAanvangMaterielePeriode(datumAanvangMaterielePeriode);
}
final String datumEindeVolgen = getNodeTextContent(
getActieSpecifiekePrefix() + "/brp:persoon/brp:afnemerindicaties/brp:afnemerindicatie/brp:datumEindeVolgen", xPath, node);
if (datumEindeVolgen != null) {
afnemerindicatie.setDatumEindeVolgen(datumEindeVolgen);
}
return afnemerindicatie;
}