Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
On site in Iceland the have an instrument that does chemical analysis of material they have tapped from the furnace.
This instrument uses software to draw up an report of the analysis (see attached doc on software).

This report information needs to be sent to a database whereby they can extract this information.
What I have done at the moment is I have created a webpage whereby they can upload an exported xml file of the report, this webpage then extracts the data and puts it into our server database on site.
What is required now is to automate this process, so instead of manually uploading the xml file to the webpage, an application needs to be written to monitor a folder and if any new XML files are saved to the folder the data needs to be extracted from the file/files and stored into a database.

What I have tried:

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;  
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;	


public class Dom
{

	
   public static void main (String[] args)
   
   {
   String badnumber = "MPP";
   int badnumberconverted; 
   
   try {
      badnumberconverted = Integer.parseInt(badnumber);
   }catch (NumberFormatException nfEx){
  // System.out.println("banumber*1000000");
         }
      
               DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      try {
               DocumentBuilder builder = factory.newDocumentBuilder();
               Document doc = builder.parse("repeat.xml");
               NodeList sampleIDs = doc.getElementsByTagName("Elements");
       for(int i=0;i<sampleIDs.getLength();i++){
               Node p = sampleIDs.item(i);
            if(p.getNodeType() == Node.ELEMENT_NODE){
               Element Elements = (Element) p;
               String id = Elements.getAttribute("id");
               NodeList nameList = Elements.getChildNodes();
             for(int j=0;j<nameList.getLength();j++){
               Node n = nameList.item(j);
             if(n.getNodeType() == Node.ELEMENT_NODE){
                Element name = (Element) n;
                  // System.out.println( convert(name.getTextContent()));
                System.out.println("Elements" + id + ":" + name.getTagName()+ "=" + name.getTextContent());
                  
                  }
               }
            }
         }     
      }  
               catch (ParserConfigurationException e){
      
                  e.printStackTrace();
      }
               catch (SAXException e)
      {
                  e.printStackTrace();
      }
                catch (IOException e){
         
                  e.printStackTrace();
      }
   }
   
  public static String convert(String val) 
   
   {
   
   double d = Double.parseDouble(val);
   d = d/1000000;
   
   return Double.toString(d);
   
                          
                      
        }    
   
   }
Posted
Updated 12-Feb-17 22:18pm
Comments
Garth J Lancaster 13-Feb-17 4:27am    
btw - your question had "(see attached doc on software)" - I couldn't find a link to it sorry ...

The trick is, list all your requirements, everything that can go right, everything that can go wrong, what failover/robustness you need etc

1 solution

I can't help you in java sorry - in c# I would (possibly, without knowing any more of your requirements)

a) write a 'Win32' service
b) the service would use FileSystemWatcher FileSystemWatcher Class (System.IO)[^]
c) when a file is added
ci) of the correct name
cii) with proper content
I would
d) extract & upload the xml/data and upload to your database
e) any files no matching name &/or content at ci && cii get put into an error file and reported on later
f) the service would provide (through some interface) stats on successful files vs 'error' files etc

This could be split into 'two different' services/apps, but maybe that's overkill, it depends on the volume/frequency of the uploads etc


in c#, you can use 'TopShelf' as the basis for a Service - this might be worthwhile looking at [NuGet Gallery | Topshelf.FileSystemWatcher 1.0.0.17](https://www.nuget.org/packages/Topshelf.FileSystemWatcher/) [^]


I'm pretty sure its easy enough to do in java
 
Share this answer
 
v4
Comments
Member 12997518 13-Feb-17 12:25pm    
Garth,
I have been battling to get this right for two weeks already. We have computer running XRF analysis software that constantly generate report in xml format and this is saved in specific folder on local drive. So what i need to achieve is to write program that will detect new xml file been saved extract data from this file and write it into sql server 2008 R2. Once data has been extracted xml file can be renamed. You can assist me with C#. Much appreciated for your help.
Garth J Lancaster 13-Feb-17 23:41pm    
sure, but you must appreciate Codeproject isn't a 'write code to order service' - most here (and I probably should) respond with 'try rentacoder' - Codeproject's premise is that people come here with code/items they need help on, not 'please write this from the ground up' - and, 99.9% of the people here are volunteers, most have full time jobs - I don't as it happens at the moment, so Im not earning $$

Writing a service to do as you want isn't terribly hard on the face of it - here's two links for instance

http://www.jaysonrowe.com/2012/09/using-topshelf-to-create-windows-service.html

https://github.com/mchudinov/FileWatcher/blob/master/FileWatcher/Jobs/FileProcessingJob.cs

The issue I find in a commercial sense is (as usual) in the details, or lack of them - FileSystemWatcher can be hard to deal with if for example the file gets created, added to, and multiple notifications are emitted - the program using FileSystemWatcher really needs to know when the file is 'ready for consumption' in your case uploading into the sqlserver database.

Requirements, again - do you even need real time uploading to database ? - as opposed to 'near real-time' eg, you use a 'polling' solution and check for files every (say) 15 minutes

Can you see why I'd balk at writing your code for you ? if you go to the IDesign Architects Master Course :-) one thing that is made clear, is that everything that is wrong with an implementation is the result of the failure of the Architect to adequately gather and analyse requirements

Who says 'writing a service' is correct ? would something like a workflow engine https://www.codeproject.com/Articles/1164009/Wexflow-Open-source-workflow-engine-in-Csharp be 'more suited' - again, requirements - who are the users of the system, who is going to monitor it, trouble shoot it etc - what are their needs ?

[edit] what about a COTS scheduler/workflow app - why the need to write one for yourself - as you said, this is for a client in Iceland, your profile suggests you are in/from South Africa, so, isn't support, reliability, robustness going to be a big issue ? [/edit]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900