use of javax.xml.xpath.XPathFactory in project Synthese_2BIN by TheYoungSensei.
the class libraryDOM 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("//*", new InputSource(new FileReader("library.xml")), XPathConstants.NODESET);
System.out.println("Nombre d'�l�ments : " + list.getLength());
int nombreAtt = 0;
for (int i = 0; i < list.getLength(); i++) {
nombreAtt += list.item(i).getAttributes().getLength();
}
System.out.println("Nombre Attribut : " + nombreAtt);
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.xml.xpath.XPathFactory in project Synthese_2BIN by TheYoungSensei.
the class DOMA method main.
public static void main(String[] args) throws Exception {
XPathFactory factory = XPathFactory.newInstance();
javax.xml.xpath.XPath xPath = factory.newXPath();
NodeList lists = (NodeList) xPath.evaluate("//*", new InputSource(new FileReader("library.xml")), XPathConstants.NODESET);
int compteurElem = lists.getLength();
int compteurAtt = 0;
for (int i = 0; i < lists.getLength(); i++) {
compteurAtt += ((Element) lists.item(i)).getAttributes().getLength();
}
System.out.println("Nombre d'�l�ments : " + compteurElem);
System.out.println("Nombre d'attributs : " + compteurAtt);
}
use of javax.xml.xpath.XPathFactory in project GDSC-SMLM by aherbert.
the class BatchPeakFit method itemStateChanged.
/*
* (non-Javadoc)
*
* @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
*/
public void itemStateChanged(ItemEvent e) {
// When the checkbox is clicked, create a default configuration file and update the
// GenericDialog with the file location.
Checkbox cb = (Checkbox) e.getSource();
if (cb.getState()) {
cb.setState(false);
Document doc = getDefaultSettingsXmlDocument();
if (doc == null)
return;
try {
// Look for nodes that are part of the fit configuration
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//gdsc.smlm.engine.FitEngineConfiguration//*");
// For each node, add the name and value to the BatchParameters
BatchSettings batchSettings = new BatchSettings();
batchSettings.resultsDirectory = System.getProperty("java.io.tmpdir");
batchSettings.images.add("/path/to/image.tif");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (// Only nodes with a single text entry
node.getChildNodes().getLength() == 1) {
batchSettings.parameters.add(new ParameterSettings(node.getNodeName(), node.getTextContent()));
}
}
// Save the settings file
String[] path = Utils.decodePath(configFilenameText.getText());
OpenDialog chooser = new OpenDialog("Settings_file", path[0], path[1]);
if (chooser.getFileName() != null) {
String newFilename = chooser.getDirectory() + chooser.getFileName();
if (!newFilename.endsWith(".xml"))
newFilename += ".xml";
FileOutputStream fs = null;
try {
fs = new FileOutputStream(newFilename);
xs.toXML(batchSettings, fs);
} finally {
if (fs != null) {
fs.close();
}
}
// Update dialog filename
configFilenameText.setText(newFilename);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
use of javax.xml.xpath.XPathFactory in project cloudstack by apache.
the class OvmObject method xmlToMap.
/* was String, Object before */
public <E> Map<String, E> xmlToMap(String path, Document xmlDocument) throws Ovm3ResourceException {
XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
XPath xPath = factory.newXPath();
try {
XPathExpression xPathExpression = xPath.compile(path);
NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
Map<String, E> myMap = new HashMap<String, E>();
for (int ind = 0; ind < nodeList.getLength(); ind++) {
NodeList nodeListFor = nodeList.item(ind).getChildNodes();
for (int index = 0; index < nodeListFor.getLength(); index++) {
String rnode = nodeListFor.item(index).getNodeName();
NodeList nodeListFor2 = nodeListFor.item(index).getChildNodes();
if (nodeListFor2.getLength() > 1) {
/* Do we need to figure out all the sub elements here and put them in a map? */
} else {
String element = nodeListFor.item(index).getTextContent();
myMap.put(rnode, (E) element);
}
}
}
return myMap;
} catch (XPathExpressionException e) {
throw new Ovm3ResourceException("Problem parsing XML to Map:", e);
}
}
use of javax.xml.xpath.XPathFactory in project cloudstack by apache.
the class OvmObject method xmlToString.
public String xmlToString(String path, Document xmlDocument) throws Ovm3ResourceException {
XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
XPath xPath = factory.newXPath();
try {
XPathExpression xPathExpression = xPath.compile(path);
NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
return nodeList.item(0).getTextContent();
} catch (NullPointerException e) {
LOGGER.info("Got no items back from parsing, returning null: " + e);
return null;
} catch (XPathExpressionException e) {
throw new Ovm3ResourceException("Problem parsing XML to String: ", e);
}
}
Aggregations