Read any file from DRL file Drools - java

Is there any way to read or parse any file format ex: JSON or XML from DRL file Drools ? for example:
when
SomeClass()
read any file type JSON or XML
then
Do something

No. You will need to read and parse the file as you want outside of the DRL and then feed the results of the parsing into your session.
You can also do the parsing in the RHS of a rule if you want.
Hope it helps,

Related

Java - How to extract information from many XML files in a directory and export it to an excel file

I am trying to get information from many xml files in a directory.
How can I get specific information from each one and send it to an excel file, in java?
file 1.xml
file 2.xml
file 3.xml
*********
**file.csv** or .**xls** with the information of the 'n' files XML
there are several libraries on Java that can help you to do so.
For instance, for getting information from XML you can use dom4j and extract the specific information make use of the query language XPATH, supported by the library (examples). And to read all the XML files form a directory, Java 8 has an easy way of achieving that.
Files.list(Paths.get("/path/to/xml/files"))
.map(YourXMLParser::parse)
.forEach(XLSExporter::export);
where parse method would have the signature:
public MyDataBean parse(Path path) {
InputStream inputStream = Files.newInputStream(Path);
SAXBuilder saxBuilder = new SAXBuilder(inputStream);
... <-- Making use of SAX for instance and return the read data in a custom Bean (MyDataBean)
}
As Files.list() method return Stream you can take advantage of that to use map and forEach.
Once you have the information from each XML files to you can export to XLS using the most used library in Java for it: Apache POI
I hope it can help.

Convert one json format to another in java

I am looking for a utility which converts one json format to another by respecting at the conversion definitions from a preferably xml file. Is there any library doing something like this in java ?
For example source json is:
{"name":"aa","surname":"bb","accounts":[{"accountid":10,"balance":100}]}
target json is :
{"owner":"aa-bb","accounts":[{"accountid":10,"balance":100}]}
sample config xml :
t.owner = s.name.concat("-").concat(surname)
t.accounts = t.accounts
Ps:Please dont post solutions for this example, it is just for giving an idea, there will be quite different scenarios in mapping.
Is this what u need?
Open input file.
Read / parse JSON from file using a JSON library.
Convert in-memory data structure to new structure.
Open output file
Unparse in-memory data structure to file using JSON library.

How to check uploaded xml is valid using java [duplicate]

This question already has an answer here:
Native Java API for Checking Valid XML
(1 answer)
Closed 9 years ago.
I am working on XML parsing in JAVA. While uploading the file i can able to validate whether it is an valid XML file or not. But if i am saving some other text file with extension(XML) how i can able to check uploaded xml is valid XML or not.
Please anyone help on this...Thanks in advance
XElement element;
TRY {
element=XElement.Load(filename);
-- Add code to upload file here
--Because if you reach here then it meant your file successfully
} CATCH (exception ex) {
-- If the code fail put code to handle the fail here.
}
Validness of an XML file has two aspects:
Well-formed XML file: XML file conforms to the general rules of XML (XML shall have a root element, attributes shall be defined double quot etc.)
Valid XML file: XML file conforms to a specific schema (DTD or XSD)
By the term "valid", I think you mean "well-formed" XML file. The only way to completely determine whether a XML file is well-formed or not is to fully parse it.
XML file is a simple text file. Not a good way but you can check '<' character as the first character. Whether XML declaration or root element, after empty spaces, every XML file starts with '<' character.
Assuming that you know the encoding of the file, you can determine whether your text file is not an XML file (but not the other way) by this way.

parse XML to POJO using XStream

I have an xml file which I want to read and parse it into POJO.
I am using XStream for this.
I am not able to send the file as an input to the code for parsing(file is on my local drive).
How to read the xml file and parse it using fromXML() method ?
I would be gratefull if someone can give example for sending xml file as input and parsing it to POJO and printing it on the screen
Thanks...
Based on the provided comments, I can better understand the problem you are facing. I believe the answer you are looking for is implicit collections. Check out the XStream tutorial on aliasing: http://x-stream.github.io/alias-tutorial.html .

Java store xml file contents into a string

I have a static method that creates an xml file and I would like it to return the raw xml file as a string. After creating the xml file I would like to read from the file and convert it to a string. How do I go about doing so?
http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/Files.html
Joiner.on('').join(Files.readLines(file, CharSet.fromName("UTF-8")))
you could turn to, for the file handling, to the apache.commons.io library.
This one has build in convenience functions for reading and storing files.
So for reading
org.apache.commons.io.FileUtils#readFileToString(File file)
and for writing
org.apache.commons.io.FileUtils#writeStringToFile(File file, String data)
See here for javadoc
http://commons.apache.org/io/
Here is a variety of ways of doing it:
How to create a Java String from the contents of a file

Categories