--------
2.java code to parse the data. here ThorImageExperimentXML is pojo class having all related properties
@Override
public ThorImageExperimentXML getExperimentInformationFromXML(String xmlPath) {
// TODO Auto-generated method stub
ParseXMLFile(xmlPath);
webserviceImageProcessDao.saveXMLExperiment(thorImageExperimentXML);
return thorImageExperimentXML;
}
private ThorImageExperimentXML ParseXMLFile(String xmlFileName) {
Node root;
try {
Document doc = parseFile(xmlFileName);
root = doc.getDocumentElement();
return writeDocumentToOutput(root, 0);
} catch (Exception e) {
e.printStackTrace();
} finally {
root = null;
}
return writeDocumentToOutput(root, 0);
}
private Document parseFile(String fileName) {
Document doc = null;
try {
DocumentBuilder docBuilder;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilder = docBuilderFactory.newDocumentBuilder();
File sourceFile = new File(fileName);
try {
doc = docBuilder.parse(sourceFile);
sourceFile = null;
} catch (SAXException e) {
return null;
}
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Error occured while parsing xml "
+ e.getMessage());
}
}
return doc;
}
private ThorImageExperimentXML writeDocumentToOutput(Node node, int indent) {
String value = "";
try {
// get element name
String nodeName = node.getNodeName();
// get element value
String nodeValue = getElementValue(node);
// get attributes of element
NamedNodeMap attributes = node.getAttributes();
if (nodeName.toString().equals("Name")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("name")) {
System.out.println("The Exact attribute Name== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setName(attribute.getNodeValue().toString());
}
}
}
if (nodeName.toString().equals("Date")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("date")) {
System.out.println("The Exact attribute Date== "
+ attribute.getNodeValue().toString().replace("/", "-"));
//java.sql.Date.valueOf(attribute.getNodeValue().toString().replace("/", "-"))
thorImageExperimentXML.setDate(attribute.getNodeValue().toString());
}
}
}
if (nodeName.toString().equals("User")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("name")) {
System.out.println("The Exact attribute User== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setUserName(attribute.getNodeValue().toString());
}
}
}
if (nodeName.toString().equals("Computer")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("name")) {
System.out.println("The Exact attribute Computer== "
+ attribute.getNodeValue().toString());
}
thorImageExperimentXML.setComputerName(attribute.getNodeValue().toString());
}
}
if (nodeName.toString().equals("Software")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("version")) {
System.out.println("The Exact attribute Software== "
+ attribute.getNodeValue().toString());
}
thorImageExperimentXML.setSoftwareVersion(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
if (nodeName.toString().equals("Camera")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("name")) {
System.out.println("The Exact attribute Camera== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setCameraName(attribute.getNodeValue().toString());
}
if (attribute.getNodeName().toString().equals("width")) {
System.out.println("The Exact attribute Camera== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setCameraWidth(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals("height")) {
System.out.println("The Exact attribute Camera== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setCameraHeight(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString()
.equals("pixelSizeUM")) {
System.out.println("The Exact attribute Camera== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setCameraPixelSizeUM(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
}
if (nodeName.toString().equals("Magnification")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("mag")) {
System.out.println("The Exact attribute Magnification== "
+ attribute.getNodeValue().toString());
}
thorImageExperimentXML.setMagnificationMag(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
if (nodeName.toString().equals("Wavelength")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("name")) {
System.out.println("The Exact attribute Wavelength== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setWavelengthName(attribute.getNodeValue().toString());
}
if (attribute.getNodeName().toString().equals(
"exposureTimeMS")) {
System.out.println("The Exact attribute Wavelength== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setWavelengthExposureTimeMS(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
}
if (nodeName.toString().equals("ZStage")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("name")) {
System.out.println("The Exact attribute ZStage== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setZstageName(attribute.getNodeValue().toString());
}
if (attribute.getNodeName().toString().equals("steps")) {
System.out.println("The Exact attribute ZStage== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setZstageSteps(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals("stepSizeUM")) {
System.out.println("The Exact attribute ZStage== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setZstageStepSizeUM(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
}
if (nodeName.toString().equals("Timelapse")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("timepoints")) {
System.out.println("The Exact attribute Timelapse== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setTimelapseIntervalSec(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString()
.equals("intervalSec")) {
System.out.println("The Exact attribute Timelapse== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setTimelapseTimePoints(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
}
if (nodeName.toString().equals("Sample")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("type")) {
System.out.println("The Exact attribute Sample type== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setSampleType(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals("offsetXMM")) {
System.out.println("The Exact attribute Sample X== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setSampleOffsetXMM(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals("offsetYMM")) {
System.out.println("The Exact attribute Sample Y== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setSampleOffsetYMM(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
}
if (nodeName.toString().equals("Wells")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("rows")) {
System.out.println("The Exact attribute Wells== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setWellsRows(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals("columns")) {
System.out.println("The Exact attribute Wells== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setWellsColumns(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals(
"wellOffsetXMM")) {
System.out.println("The Exact attribute Wells== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setWellsWellOffsetXMM(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals(
"wellOffsetYMM")) {
System.out.println("The Exact attribute Wells== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setWellsWellOffsetYMM(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
}
if (nodeName.toString().equals("SubImages")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("subRows")) {
System.out.println("The Exact attribute SubImages== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setSubimagesSubRows(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals("subColumns")) {
System.out.println("The Exact attribute SubImages== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setSubimagesSubColumns(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals(
"transOffsetXMM")) {
System.out.println("The Exact attribute SubImages== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setSubimagesTransOffsetXMM(Float.parseFloat(attribute.getNodeValue().toString()));
}
if (attribute.getNodeName().toString().equals(
"transOffsetYMM")) {
System.out.println("The Exact attribute SubImages== "
+ attribute.getNodeValue().toString());
thorImageExperimentXML.setSubimagesTransOffsetYMM(Float.parseFloat(attribute.getNodeValue().toString()));
}
}
}
if (nodeName.toString().equals("Comments")) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().toString().equals("text")) {
System.out.println("The Exact attribute Comments== "
+ attribute.getNodeValue().toString());
}
thorImageExperimentXML.setCommentsText(attribute.getNodeValue().toString());
}
}
// write all child nodes recursively
NodeList children = node.getChildNodes();
// middle="";
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
// if(node.getNodeName().equals("Computer"))
valuecount = i;
writeDocumentToOutput(child, indent + 2);
}
}
nodeName = null;
nodeValue = null;
} catch (Exception e) {
if (logger.isDebugEnabled()) {
e.printStackTrace();
logger
.debug("Error occured in Comman Class while writing to document "
+ e.getMessage());
}
}
return thorImageExperimentXML;
}
private final String getElementValue(Node elem) {
try {
Node kid;
if (elem != null) {
if (elem.hasChildNodes()) {
for (kid = elem.getFirstChild(); kid != null; kid = kid
.getNextSibling()) {
if (kid.getNodeType() == Node.TEXT_NODE) {
return kid.getNodeValue();
}
}
}
}
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger
.debug("Error occured in Comman Class while getting element value "
+ e.getMessage());
}
}
return "";
}
No comments:
Post a Comment