Importing XML with DataFile
-
Hi,
I am trying to import an XML file as data source into Mango. However, I cannot file the sample Import Class Templates that is available for CSV and Excel. Can you point me to those files or give some pointers on how I can write my own?
Here is a sample of the xml file I want imported. I am interested in importing just the <assets></assets> items only.
<mobiletrak version="1.0">
<assets>
<asset name="Equipnment-0001" desc="Caterpillar Excavator" zoneid="0">
<tag id="00013934" taggroupid="RCKIRU" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0002" desc="Komatsu Forklift" zoneid="0">
<tag id="00013947" taggroupid="RCKIRU" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0003" desc="Liebherr Loader" zoneid="0">
<tag id="00013938" taggroupid="RCKIRU" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0004" desc="Volvo Road Roller" zoneid="0">
<tag id="00013944" taggroupid="RCKIRU" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0005" desc="Caterpillar Excavator" zoneid="0">
<tag id="00013949" taggroupid="RCKIRU" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0006" desc="Komatsu Forklift" zoneid="0">
<tag id="00013941" taggroupid="RCKIRU" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0007" desc="Liebherr Loader" zoneid="0">
<tag id="00013942" taggroupid="RCKIRU" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0008" desc="Volvo Road Roller" zoneid="0">
<tag id="00013939" taggroupid="RCKIRU" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0009" desc="Liebherr Loader" zoneid="0">
<tag id="00255435" taggroupid="RCKIRT" lastseen="63602187713"/>
</asset>
<asset name="Equipnment-0010" desc="Volvo Road Roller" zoneid="0">
<tag id="00436168" taggroupid="RCKIRT" lastseen="63602187713"/>
</asset><asset name="Equipnment-0011" desc="Caterpillar Excavator" zoneid="0"> <tag id="00013933" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0012" desc="Komatsu Forklift" zoneid="0"> <tag id="00013935" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0013" desc="Liebherr Loader" zoneid="0"> <tag id="00013936" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0014" desc="Volvo Road Roller" zoneid="0"> <tag id="00013937" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0015" desc="Caterpillar Excavator" zoneid="0"> <tag id="00013940" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0016" desc="Komatsu Forklift" zoneid="0"> <tag id="00013943" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0017" desc="Liebherr Loader" zoneid="0"> <tag id="00013945" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0018" desc="Volvo Road Roller" zoneid="0"> <tag id="00013946" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0019" desc="Liebherr Loader" zoneid="0"> <tag id="00013948" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> <asset name="Equipnment-0020" desc="Volvo Road Roller" zoneid="0"> <tag id="00013950" taggroupid="RCKIRU" lastseen="63602187713"/> </asset> </assets> <zones> <zone id="27" name="Zone A"/> <zone id="66" name="Zone B"/> <zone id="39" name="Zone C"/> <zone id="28" name="Zone D"/> </zones> <taggroups> <taggroup id="RCKIRU" treatcode="04W" family=""/> <taggroup id="RCKIRT" treatcode="04W" family=""/> </taggroups>
</mobiletrak>
-
Hi Ricardo,
How strange there isn't one in the module. I'll have to look for the old example or write a new one! The context help ? in Mango does have an explanation of the method, but I'll try to give some details here.
Process:
- Use and online schema generator to get a schema for your sample XML document, this should be an .xsd file.
- Run xjc from your Java JDK against the xsd schema file to get an unmarshaller class.
- Modify this automatically generated class, add "implements AbstractXMLDataSource" to the class definition.
- Implement the "public List<ImportPoint> getParsedPoints()" method. For your case this will probably involve a call to another method in the class named something automatically like "getAssets" and you can loop over the list of asset objects getting the information/identifiers required to build your ImportPoint list. There are BinaryimportPoint, MultistateImportPoint, NumericImportPoint, and AlphanumericImportPoint each with four arguments: String identifier, T value, long time, Map<String, String> extraparams (you can just use an empty String-String Map).
- Add this extended unmarshaller class somewhere accessible and load it from the data source.
-
Hi Philip,
Are you able to locate the XML example?
BR,
Ricardo -
Hi Philip,
I have put together the unmarshaller class, but I do not know how to compile it from .java to .class. As it needs to import package such as com.infiniteautomation.datafilesource.dataimage.NumericImportPoint. Please advise.
BR,
Ricardo -
You need to put the lib for the module on the classpath. So, "-cp Mango/web/modules/dataFile/lib/*"
-
I was able to locate this class from an XML data source:
0_1467212606429_TrendReport.javaThe main two parts are:
public class TrendReport implements AbstractXMLDataSource {
and then the function contained in AbstractXMLDataSource:
@Override public List<ImportPoint> getParsedPoints() { List<ImportPoint> ans = new ArrayList<ImportPoint>(); SimpleDateFormat dtf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); try { for(TrendReport.TrendOutput.TrendValues tv : this.getTrendOutput().getTrendValues()) { Map<String, String> params = new HashMap<String, String>(); Date dt = dtf.parse(tv.getDateTime()); String fqr = tv.getFqr(); params.put("identifier", fqr); params.put("deviceName", fqr.split(" ")[0]); params.put("name", fqr.substring(fqr.indexOf("."), fqr.lastIndexOf("."))); params.put("xid", "generate"); ans.add(new NumericImportPoint(fqr, Double.valueOf(tv.getObjectValue().split(" ")[0]), dt.getTime(), params)); } } catch(ParseException e) { e.printStackTrace(); } return ans; }
-
Hi Philip,
Thank you for the example class. Can you share the XML file too?
BR,
Ricardo -
I can share the XSD, which was created by just copying the XML into an online XSD generator. Then you use JAXB to generate the class given in the last post, before adding the function mentioned.. 0_1467214195787_trendReport.xsd