java.io.File Class

I tried to read/write files in rules.

Have anybody tried to use java.io.File class ?

Ok
was not so hard …

but mo way to import javax.xml.parsers.DocumentBuilderFactory.

give me your tought please about this.
Thank’s

rule "file"
when
   Time cron "0 * * * * ?" or Item inc_alarm received update
then
	var File fXmlFile = new File("/home/pi/staff.xml")
	var DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance()
	var DocumentBuilder dBuilder = dbFactory.newDocumentBuilder()
	var Document doc = dBuilder.parse(fXmlFile)
	var doc.getDocumentElement().normalize()

	println("Root element :" + doc.getDocumentElement().getNodeName())

	var NodeList nList = doc.getElementsByTagName("staff")

	println("----------------------------")

	for (int temp = 0; temp < nList.getLength(); temp++) {

		var Node nNode = nList.item(temp)

		println("\nCurrent Element :" + nNode.getNodeName())

		if (nNode.getNodeType() == Node.ELEMENT_NODE) {

			var Element eElement = (Element) nNode

			println("Staff id : " + eElement.getAttribute("id"))
			println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent())
			println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent())
			println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent())
			println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent())

		}
end 

Would the XPath transformation be easier to use?

    val xml = executeCommandLine("cat /home/pi/staff.xml", 5000)

    val Number numStaff = new  Integer(transform("XPATH", "count(//staff)"))

    val int i = 1
    while(i <= numStaff) {
        var id = transform("XPATH", "//staff["+i+"]/@id")  // return the id attribute of the first staff element
        var first = transform("XPATH", "//staff["+i+"]/firstname")
        ...
        i = i + 1
    }

Note: I just typed the above in and I think Xpath only works in OH 2. You should be able to do something similar with XSLT, though it will require a bunch of separate files to extract all the data you are after. It will probably work better if I knew the root element which would go between the first two //

The first line gets the file into a String using the executeCommandLine Action which is way easier IMHO than trying to load the file using java.io classes.

Hi
Yes much easier this way.

My arduino sends me the alarms and I catch the strings to send stored in a XML file…

Thanks

rule "Alarm management"
when
   Item inc_alarm received update
then

	var String element = inc_alarm.state.toString
	var String [] parts = element.split("|")

	var int dev_num = Integer::parseInt(parts.get(1)).intValue
	var int fct_num = Integer::parseInt(parts.get(3)).intValue
	var int alarm_num = Integer::parseInt(parts.get(5)).intValue
	if (alarm_num == 3){
		publish("mymosquitto","homehorn/RF/C/1/0/1","1")
	}
	val xml = executeCommandLine("cat /home/pi/Equipements.xml", 5000)

	var device= transform("XPATH","//device_lib[@dev="+dev_num+"]",xml)
    logInfo("XPATH","device = " + device)
    var lib_fct= transform("XPATH","//lib_code_function[@dev="+dev_num+"][@fct="+fct_num+"]",xml)
    logInfo("XPATH","lib_fct = " + lib_fct)
    var lib_alert= transform("XPATH","//lib_alert[@dev="+dev_num+"][@fct="+fct_num+"]",xml)
    logInfo("XPATH","lib_fct = " + lib_alert)
    var msg_alert= transform("XPATH","//alert[@dev="+dev_num+"][@fct="+fct_num+"][@levl="+alarm_num+"]",xml)
    logInfo("XPATH","msg_alert = " + msg_alert)

    var String msg_push = device + " signale un(e) " + lib_alert + "  : " + msg_alert
end