I'm looking for a leightweight library that allows me to genereate an XSD from XML in Java (no commandline tool). I know that it is not a clean way to generate it, but in this case I need to do it. Also the XML is very simple in terms of structure.
I already looked into Trang, but there is no API documentation except how to call it from command line.
Also I checked out xsd-gen, but the issue with that library is that one would need to modify some package declrations in the source code which I couldn't find.
Any other suggestions?
I am the author of the tool xsd-gen. I converted the tool to be a library as well, and uploaded the artifact to Maven Central:
<dependency>
<groupId>org.wiztools</groupId>
<artifactId>xsd-gen</artifactId>
<version>0.2.1</version>
</dependency>
Now it is simple to use as a library within your application:
import org.wiztools.xsdgen.XsdGen;
import java.io.File;
import java.io.FileOutputStream;
...
XsdGen gen = new XsdGen();
gen.parse(new File("in.xml"));
File out = new File("out.xsd");
gen.write(new FileOutputStream(out));
I included the xsd-gen source code and it worked for me. You only need
TypeInferenceUtil.java
XsdGen.java
The package declarations I used (for Gradle) were:
compile("com.io7m.xom:xom:1.2.10")
compile("org.wiztools.commons:wiztools-commons-lib:0.4.1")
Related
Maybe I am misunderstanding Maven's dependency principles but this is my question:
I have a little Java program that requires these imports
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
Now instead of importing these at the top of the code, I would just go
into the POM file of my Maven project and add the dependencies.
But on https://mvnrepository.com/ how do I find the correct imports?
Is there another way besides looking on that site?
Thank you.
Now instead of importing these at the top of the code, I would just go into the POM file of my Maven project and add the dependencies.
No. You are conflating two different things:
Managing dependencies (downloading and placing libraries within your project)
Using dependencies (calling the library’s classes and methods from within your code)
Managing dependencies
To use a library, you need to obtain a physical copy, a file, usually a .jar file. You can manually download a copy. Or you can use Maven or Gradle to download a copy on your behalf. The Maven or Gradle approach is generally recommended over the manual approach.
Once downloaded, you need to place the file where it can be found within your project. Again, you can do this manually, or you can use Maven or Gradle to make the file available to your project. Again, the Maven or Gradle approach is generally recommended over the manual approach.
Using dependencies
After having obtained and placed a copy of the library, you are ready to access its classes and methods.
👉 The catch is that the authors of that library may have named some of their classes and methods coincidentally with the same name as found in another library.
Imagine you want to use a class named Source, but two of your libraries have such a class:
javax.xml.transform.Source
com.example.awesome.Source
If you write in your code:
Source s = new Source() ;
… how does the compiler know which of the two classes you meant? 👈
To resolve the mystery, you either:
Write a fully-qualified class name.javax.xml.transform.Source s = new javax.xml.transform.Source() ;
Write an import statement.import javax.xml.transform.Source ;
The second approach, writing an import statement, usually makes for less typing and easier reading than the first approach of using fully-qualified names.
The word import is a bit of a misnomer, and was used for legacy historical reasons. Its use here does not involve any moving of anything anywhere. A better name would have been namespace, as in, specifying a defined domain of known names.
When reading this:
import javax.xml.transform.Source ;
… think this:
namespace javax.xml.transform.Source ;
… meaning: “Any use below of the word “Source” can be assumed to mean the Source class from the library whose package is javax.xml.transform”
In its effort to find the class of that package you named, the Java Virtual Machine at runtime automatically looks through all the libraries you obtained and placed.
There is something called java standard library - this means that a lot of things are automatically avaibable to you and you don't have to add anything to pom file.
Maven is used for adding external libraries that are not included and shipped with your java.
Easiest way to find out if you need to add anything to pom file is to use good IDE (for example Intellij) that provides support for maven. It should mark any libraries that are missing - then add those to your pom file.
And you still need import everything you need to use for each *.java file.
You can also search Maven library by full class name by fc operator at search.maven.org
eg.
fc:javax.xml.transform.stream.StreamResult
https://search.maven.org/search?q=fc:javax.xml.transform.stream.StreamResult
Of course one class can be found in many library...
I have an AnyLogic model that uses JAXB functions for parsing an XML file. The model used to work earlier but it doesn't now since apparently the newer versions of Java don't include JAXB. The online examples of how to do this in Java programs doesn't fit AnyLogic environment.
Based on online searches, I have downloaded and included the jaxb-api-2.4.0-b180830.0359.jar file in AnyLogic model. That by itself doesn't work and leads to the following error:
SEVERE: null
javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
I then added the following in the import section:
import java.xml.bind;
import com.sun.xml.bind;
Also tried:
import java.xml.bind.*;
import com.sun.xml.bind.*;
Both resulted in the same error:
The import com.sun.xml.bind cannot be resolved.
The import java.xml cannot be resolved.
On line guidance from for example https://www.dariawan.com/tutorials/java/using-jaxb-java-11/ recommend adding the dependencies using below code to Java programs:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
How do we specify such dependencies via AnyLogic interface?
Once JAXB functions work, they should lead to parsing the data in XML file and creating and populating corresponding objects in AnyLogic as was the case before.
Sounds like you didn't add the .jar file properly in AnyLogic...
Click on your model in the projects view (the top-most entry above all agents and Main):
In the properties, you can add your .jar file under the "Dependencies" tab:
Do follow the advise to "keep a copy in your model folder" so it doesn't get lost.
Now you can import what you need and it will work (unless the .jar file is broken itself).
PS: Might be better to rephrase your question to "How to load an external jar file to my model dependencies"
The problem
As already stated in the question, the problem is this:
JAXB as a Java tool for XML treatment has been removed from the Java standard library beginnig with Java 9. The reason for this decision was to make the standard Java library more lightweight.
Dependencies in Java
This can be solved by including the removed package(s) manually into your project. As a lot of people already had this problem, quite an amount of SO questions for this exist, like this and this.
In these answers it is suggested to 'load dependecies', eg like this:
<!-- https://mvnrepository.com/artifact/pacakgename -->
<dependency>
<groupId>packagename</groupId>
<artifactId>modulename</artifactId>
<version>1.0.0</version>
</dependency>
These dependency statements are interpreted by the Java IDE such as Eclipse (with a package manager such as Maven), and the stated packages then get automatically loaded from an online package repository and included in the project.
In AnyLogic, the procedure is slightly different!
Dependencies in AnyLogic
In AnyLogic the exact same thing is happening when you click on your project in the AnyLogic editor, and add a JAR file under the Dependencies tab, see Benjamin's answer for this. In order to do that you will manually have to find the JAR file in a package repository and download it first.
Needed Pacakges
This is where I am not completely sure. I'll got an example to run when I included the following packages, but probably there is redundancy, so you might want to try around with variations of them:
javax.xml.bind / jaxb-api / 2.3.0-b170201.1204
javax.activation / activation / 1.1
org.glassfish.jaxb / jaxb-runtime / 2.3.0-b170127.1453
com.sun.xml.bind / jaxb-impl / 2.2.11
com.sun.xml.bind / jaxb-core / 2.2.11
Example
I created a simple example model in AnyLogic, that is based on this blog post. You can run and download it (including the dependeny Java packages) here.
We are evaluating FlatBuffers as a potential solution for packing and unpacking various data payloads. I have built flatc.exe, constructed schemas for our data, and generated Java code from the schemas. I am now trying to use the generated code.
This overview states:
Then you can include both FlatBuffers and the generated code to read or write a FlatBuffer.
And does so as follows in the example:
import MyGame.Example.*;
import com.google.flatbuffers.FlatBufferBuilder;
1) Should the generated code be imported as a new module/Java Library, dependency, or something else? How is this done?
2) The import of com.google.flatbuffers.FlatBufferBuilder also does not resolve. Anyone know if this reference has changed?
Appreciate any help you can provide to an Android neophyte on how to import these items.
Thanks.
FlatBuffers doesn't come with integrations for any specific IDE's or package managers (which is something we should fix). For now the easiest is to copy the contents of FlatBuffers java/ folder to where-ever you keep your project's Java code, along with the generated code (in its package directory).
So i am using gradle to get dependencies from maven central which is working fine. I just don't know how to import them to my actual java file.
How do i found out the name to import it?
at the top of my java file i have to write
import <name>
How do i find the name?
Thank You.
According to your comments. You have to import the packages, which are contained within the library, not the library itself. There is no guarantee, this package names are the same as group or artifact id of the library. To get know that package names, usually you may use a javadocs for the library. Or just simply let your IDE to make it for you, them you're trying to use some classes from that lib.
Alternatively, you can use some off-sites, like mvnrepository.com, where you may find your library and take a look at the packages list within it. For example, description for Apache Commons Lang library, where you can see the "Packages" section with all the packages within the lib. You may import them, just like:
import org.apache.commons.lang3.*;
One more solution, is to unzip a jar and take a look into it's content to determine the packages structure.
I have a simple XML file that I have parsed to JSON. All is fine and dandy, I have a Java class that is stand alone (i.e. it has a public static void main (String args[])....)
This has a private constructor (because I need to call it with Strings either a filename or the actual data). So I have two methods that return an instance of the object. I know a bit of Java as you can tell.
OK. When I run the code in Eclipse that runs the main method my file is loaded and decoded as required. It also works for a raw String that I run via JUnit.
So I know the following facts -
the parsing of a static String works and decodes perfectly
if I provide a file it is loaded and decoded correctly.
Now the issue:
As soon as I run it in Spring framework I can write to standard out the entire file content that I have run via the stand alone code.
But before it can run anything at all I get the below error -
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:920)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
The stand alone code is run in Eclipse, and the Spring is run pointing to that code using Tomcat 7.
Why is it not finding the ParsException correctly?
The imports in the calling Spring controller are
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Is their a way of altering the build order, and would that fix it?
Looks like you have missed including the json-simple-.jar in your classpath. Include the same and it should get solved.
Hope this helps !
Add json-simple-1.1.1.jar file in your build path. On Eclipse, right click on project-> build path -> configure build path -> java build path -> add external jars -> select the jar file.
Add build path entry into deployment assembly. On the same properties window, Select "deployment assembly" option. Then add "Java Build Path" entries. This should show you the build path entry you just made in step one.
you are missing a jar deployment.
it is related for Tomcat7. A few jar needed to be exported ( forgot which ones) other web server doesn't need but Tomcat.
Could you please add this jar json-simple-1.1.1.jar in your classpath and try it out ?
This jar can be downloaded at http://code.google.com/p/json-simple/downloads/detail?name=json-simple-1.1.1.jar
The solution that I used in the end was to change to sourceforge.net json parser, a couple of code tweaks and my JUnit tests still worked and Tomcat did not complain. This may not be the best solution but it worked.
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
I have had a similar error and spent 3 4 hours removing and adding new jar files. What my problem was that i added jar file directly from the download location ie THE ZIP files ..!!! after adding a proper extracted jar file , My problem seems to be resolved. Hope this helps someone..!!
IF you are making an AI in Spring as I was when I had this error:
You also need to add the Jar to your AI "jlib" folder, for instance "...\AI\Skirmish\MyAI\0.1\jlib".
It's not enough to add it in Eclipse or Netbeans and build the project. Spring must have the jar too.
I know it is a late answer it but can help other people with the same error.
Use the below java library for process JSONArray and JSONObject
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
Just add maven dependency of JSON simple in your pom.xml. No need to configure any classpath.
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>