use of javax.xml.xpath.XPathFactory in project Synthese_2BIN by TheYoungSensei.
the class libraryDOM2 method main.
public static void main(String[] args) {
try {
XPathFactory factory = XPathFactory.newInstance();
javax.xml.xpath.XPath xPath = factory.newXPath();
NodeList list = (NodeList) xPath.evaluate("//list", new InputSource(new FileReader("library.xml")), XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) {
Element elem = (Element) list.item(i);
System.out.println(elem.getAttribute("name"));
NodeList tracks = elem.getElementsByTagName("track_ID");
for (int j = 0; j < tracks.getLength(); j++) {
Element track = (Element) tracks.item(j);
Node artist = (Node) xPath.evaluate("//song[track_ID = " + track.getTextContent() + "]/artist", new InputSource(new FileReader("library.xml")), XPathConstants.NODE);
Node name = (Node) xPath.evaluate("//song[track_ID = " + track.getTextContent() + "]/name", new InputSource(new FileReader("library.xml")), XPathConstants.NODE);
System.out.println(artist.getTextContent() + " - " + name.getTextContent());
}
System.out.println("----------------------------------");
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.xml.xpath.XPathFactory in project jangaroo-tools by CoreMedia.
the class PomConverter method changePackaging.
/**
* Changes the packaging from jangaroo to jangaroo-pkg in {@code /project/packaging}
*/
private static void changePackaging(Document document) throws MojoExecutionException {
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
Node packagingNode = (Node) xPath.evaluate("/project/packaging[text() = 'jangaroo']", document, NODE);
if (packagingNode != null) {
packagingNode.setTextContent("jangaroo-pkg");
}
} catch (XPathException e) {
throw new MojoExecutionException("error while generating modified POM", e);
}
}
use of javax.xml.xpath.XPathFactory in project jangaroo-tools by CoreMedia.
the class PomConverter method removeExmlPlugin.
/**
* Replaces exml-maven-plugin configuration by jangaroo-maven-plugin configuration within
* {@code /project/build/plugins} and {@code /project/build/pluginManagement/plugins}.
*/
private static void removeExmlPlugin(Document document) throws MojoExecutionException {
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
Node pluginsNode = (Node) xPath.evaluate("/project/build/plugins", document, NODE);
removeExmlPlugin(pluginsNode);
pluginsNode = (Node) xPath.evaluate("/project/build/pluginManagement/plugins", document, NODE);
removeExmlPlugin(pluginsNode);
} catch (XPathException e) {
throw new MojoExecutionException("error while generating modified POM", e);
}
}
use of javax.xml.xpath.XPathFactory in project perun by CESNET.
the class ExtSourceXML method getValueFromXpath.
/**
* Get xml Node and xpath expression to get value from node by this xpath.
*
* @param node node for getting value from
* @param xpathExpression expression for xpath to looking for value in node
* @return string extracted from node by xpath
* @throws InternalErrorException
*/
protected String getValueFromXpath(Node node, String xpathExpression) throws InternalErrorException {
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr;
try {
expr = xpath.compile(xpathExpression);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query.", ex);
}
String text;
try {
text = (String) expr.evaluate(node, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on node.", ex);
}
return text;
}
use of javax.xml.xpath.XPathFactory in project perun by CESNET.
the class ExtSourceXML method xpathParsing.
/**
* Get query and maxResults.
* Prepare document and xpathExpression by query.
* Get all nodes by xpath from document and parse them one by one.
*
* The way of xml take from "file" or "uri" (configuration file)
*
* @param query xpath query from config file
* @param maxResults never get more than maxResults results (0 mean unlimited)
*
* @return List of results, where result is Map<String,String> like <name, value>
* @throws InternalErrorException
*/
protected List<Map<String, String>> xpathParsing(String query, int maxResults) throws InternalErrorException {
//Prepare result list
List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
//Create new document factory builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new InternalErrorException("Error when creating newDocumentBuilder.", ex);
}
Document doc;
try {
if (file != null && !file.isEmpty()) {
doc = builder.parse(file);
} else if (uri != null && !uri.isEmpty()) {
doc = builder.parse(this.createTwoWaySSLConnection(uri));
} else {
throw new InternalErrorException("Document can't be parsed, because there is no way (file or uri) to this document in xpathParser.");
}
} catch (SAXParseException ex) {
throw new InternalErrorException("Error when parsing uri by document builder.", ex);
} catch (SAXException ex) {
throw new InternalErrorException("Problem with parsing is more complex, not only invalid characters.", ex);
} catch (IOException ex) {
throw new InternalErrorException("Error when parsing uri by document builder. Problem with input or output.", ex);
}
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression queryExpr;
try {
queryExpr = xpath.compile(query);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query.", ex);
}
//Call query on document node and get back nodesets
NodeList nodeList;
try {
nodeList = (NodeList) queryExpr.evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on document.", ex);
}
//Test if there is any nodeset in result
if (nodeList.getLength() == 0) {
//There is no results, return empty subjects
return subjects;
}
//Iterate through nodes and convert them to Map<String,String>
for (int i = 0; i < nodeList.getLength(); i++) {
Node singleNode = nodeList.item(i);
// remove node from original structure in order to keep access time constant (otherwise is exp.)
singleNode.getParentNode().removeChild(singleNode);
Map<String, String> map = convertNodeToMap(singleNode);
if (map != null)
subjects.add(map);
//Reducing results by maxResults
if (maxResults > 0) {
if (subjects.size() >= maxResults)
break;
}
}
this.close();
return subjects;
}
Aggregations