So I'm transitioning over from C# in Unity, where reflection in XML was quite easy. I wanted to apply the same process in Java to this to a slightly varied version of already existing XML of this type of format:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<imgdir name="0206.img">
<imgdir name="02060003">
<imgdir name="info">
<canvas name="icon" width="32" height="32">
<vector name="origin" x="0" y="32" />
</canvas>
<canvas name="iconRaw" width="32" height="31">
<vector name="origin" x="0" y="32" />
</canvas>
<int name="price" value="20" />
<int name="slotMax" value="1000" />
<int name="incPAD" value="4" />
</imgdir>
<imgdir name="bullet">
<canvas name="0" width="43" height="18">
<vector name="origin" x="23" y="10" />
<int name="z" value="0" />
</canvas>
<canvas name="1" width="41" height="9">
<vector name="origin" x="22" y="5" />
<int name="z" value="0" />
</canvas>
</imgdir>
</imgdir>
<imgdir name="02060005">
<imgdir name="info">
<canvas name="icon" width="26" height="25">
<vector name="origin" x="-3" y="25" />
<int name="z" value="0" />
</canvas>
<canvas name="iconRaw" width="20" height="18">
<vector name="origin" x="-6" y="25" />
<int name="z" value="0" />
</canvas>
<int name="slotMax" value="800" />
<int name="incPAD" value="10" />
<int name="reqLevel" value="10" />
<int name="tradeBlock" value="1" />
</imgdir>
<imgdir name="bullet">
<canvas name="0" width="36" height="19">
<vector name="origin" x="22" y="9" />
<int name="delay" value="150" />
</canvas>
<canvas name="1" width="40" height="22">
<vector name="origin" x="20" y="11" />
<int name="delay" value="150" />
</canvas>
<canvas name="2" width="43" height="21">
<vector name="origin" x="21" y="10" />
<int name="delay" value="150" />
</canvas>
<canvas name="3" width="46" height="23">
<vector name="origin" x="20" y="11" />
<int name="delay" value="150" />
</canvas>
</imgdir>
<imgdir name="hit">
<canvas name="0" width="55" height="38">
<vector name="origin" x="13" y="20" />
<int name="delay" value="90" />
</canvas>
<canvas name="1" width="43" height="55">
<vector name="origin" x="17" y="29" />
<int name="delay" value="100" />
</canvas>
<canvas name="2" width="51" height="65">
<vector name="origin" x="26" y="34" />
<int name="delay" value="100" />
</canvas>
<canvas name="3" width="62" height="87">
<vector name="origin" x="34" y="57" />
<int name="delay" value="100" />
</canvas>
</imgdir>
</imgdir>
The catch is that there are many of these XML files, and each one has different header names, such as 0206.img, 0207.img. Each one has a very similar structure to the previous one in that header category (leading digit determines what type of object it is unmarshaling).
I really only want to reflect properties such as price, slotMax, incPad, and ignore the rest if possible. I'm not sure how to go about this, since in all the unmarshal examples the XML is much simpler, such as
<Employees>
<Employee>
<id>1</id>
</Employee>
<Employee>
<id>2</id>
</Employee>
</Employees>
Where you'd probably define a class like Employees which contains List, and Employee can be unmarshalled into. To clarify, I know you have to set the RootElement to the name of the top element, such as in this case 0206.img, but how can this be dynamic for 0207?
As for code ... I mean the code base is pretty simple since it's just defining the structure for which to unmarshal into. Defining this structure in respect to the above XML I linked is harder for me atm.
If you're only interested in a few easy-to-detect elements, and not in the whole structure, JAXB may be a wrong tool. JAXB is good for the cases where you have an XML Schema, interested in (more or less) complete structure, unmarshalling as well as marshalling.
If you only need a few parts of XML there are easier ways to do this. In this particular case I would probably use STAX. Something along the lines:
private static final QName INT_ELEMENT_NAME = new QName("int");
// ...
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(is);
while (reader.hasNext()) {
if (XMLStreamConstants.START_ELEMENT == reader.next()) {
if (Objects.equals(INT_ELEMENT_NAME, reader.getName())) {
String name = reader.getAttributeValue(null, "name");
String value = reader.getAttributeValue(null, "value");
System.out.println(name + "=" + value);
}
}
}
Prints name/value paits from all the int elements. Can be easily extended to further cases. Very fast, does not have the JAXB overhead, would easily work with big XML files, no matter how huge.
There are other tools as well. There was one parser (I could not recall the name) where you define XPath-based rules and the parser returns data according to these rules. That would also be suitable, but I just don't recall the name. I think it was some Apache project.
Update: I've finally found it, I was talking about Digester. It allows configuring XPath-like binding rules and the creates objects for you. Still, I'd probably stay with the STAX solution unless you're sure you'll get more and more complex rules.
Related
I need to parse an xml file with the structure given below and store the data to mysql DB. But, I'm not sure from where to start, since this is a xml structure I never faced before.
Dozens of examples over the internet treats a structure where every attribute is bounded by its tag.
For instance, this is excellent starting point, but xml structure is way different than mine.
https://dzone.com/articles/load-xml-into-mysql-using-java
Any suggestion would be highly appreciated.
Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<Models>
<Model id="1">
<Nodes>
<Node i="0" key="" name="Node 0" Vb_kV="225" gsh="0" bsh="0"/>
<Node i="1" key="" name="Node 1" Vb_kV="380" gsh="0" bsh="0"/>
<Node i="2" key="" name="Node 2" Vb_kV="225" gsh="0" bsh="0"/>
</Nodes>
<Loads>
<Load node="0" key="" name="" P0="0.058" P1="0"/>
<Load node="2" key="" name="" P0="1.425" P1="0"/>
<Load node="4" key="" name="" P0="0.328" P1="0"/>
</Loads>
<Generators>
<Generator pos="0" node="1246" key="" name="""/>
<Generator pos="1" node="1333" key="" name="""/>
<Generator pos="2" node="8700" key="" name=""/>
</Generators>
<Branches>
<Branch pos="0" nodeFrom="691" nodeTo="0" key="" name="" />
<Branch pos="1" nodeFrom="260" nodeTo="1" key="" name="" />
<Branch pos="2" nodeFrom="660" nodeTo="1" key="" name="" />
</Branches>
<Measurements>
<Node>
<Meas node="1" type="Z" val="0" w="1000"/>
<Meas node="3" type="Z" val="0" w="1000"/>
<Meas node="6" type="Z" val="0" w="1000"/>
<Meas node="7" type="Z" val="0" w="1000"/>
</Node>
<Branch>
<Meas branch="0" node="691"/>
<Meas branch="0" node="691"/>
<Meas branch="1" node="260"/>
</Branch>
</Measurements>
</Model>
</Models>
In java awt I want to make a CRUD form for master detail data entry in oracle database. Gatepass master and Gatepass detail tables are joined on gatepass number using pk/fk relation.
This is form for gatepass. one gatepass receives multiple items.Format is like
Gatepass# 1 Receiving Person:Mr.Tariq
Gate pass date:21-mar-2019
Detail
item id item name qty
12 wooden chair 4
13 wooden table 1
Can any one give some sample code.
If you have more than 10 entities and using Hibernate then you can try Desktop Java Forms which I hosted at: https://github.com/smart-flex/Djf
For example here is minimalistic definition from demo-application for two master-detail relationships:
<panel constraint="Center">
<layout clazz="net.miginfocom.swing.MigLayout">
<param type="string" value="" />
<param type="string" value="[0:0, grow 70, fill][0:0, grow 30, fill]" />
<param type="string" value="[c, pref, fill] 15 [c, 150px] 10 [c, pref!]" />
</layout>
<items>
<grid bindPref="st">
<cols>
<int bind="idStreet" title="ID" width="50" enabled="no"
noResize="yes" />
<text bind="streetName" title="Street name" />
</cols>
</grid>
<grid constraint="wrap">
<cols>
<text bind="st.buildings.buildingNumber" title="Number" tips="Building number"/>
</cols>
</grid>
<grid noInfoColumn="true" bindPref="ct">
<cols>
<int bind="carId" title="ID" width="50" enabled="no"
noResize="yes" />
<text bind="carName" title="Car name" enabled="no" />
</cols>
</grid>
<grid noInfoColumn="true" constraint="wrap">
<cols>
<text bind="ct.modelList.modelName" title="Model name" enabled="no" />
</cols>
</grid>
<oper constraint="span 2"/>
</items>
</panel>
We have a project being managed by more than 20 developers and most of them use intellij.
I was wondering if we can source an xml file somehow so that every developer doesn't have to import code style settings manually.
How to properly import Java code style settings with IntelliJ?
This thread suggests that we have to manually import the xml file and only then we will see those settings will come into effect. Is there a setting that can import the code styles automatically?
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="15" />
<option name="IMPORT_LAYOUT_TABLE">
<value>
<package name="" withSubpackages="true" static="true" />
<emptyLine />
<package name="java" withSubpackages="true" static="false" />
<emptyLine />
<package name="javax" withSubpackages="true" static="false" />
<emptyLine />
<package name="org" withSubpackages="true" static="false" />
<emptyLine />
<package name="com" withSubpackages="true" static="false" />
<emptyLine />
<package name="" withSubpackages="true" static="false" />
</value>
</option>
<codeStyleSettings language="JAVA">
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
</codeStyleSettings>
</value>
</option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</component>
</project>
The above settings in .idea/codestyleSettings.xml did the trick.
I am using IntelliJ Idea 14.1.4 and Gradle, and I am trying to create sub projects and nested sub projects:
Project
|--Research
|--Development
|--Algorithms
The reason for this is that research algorithms are separate from the development implementation. They are written in different languages (Python vs. Java) and used by different programmers, however I still want to maintain one Gradle project, so eventually researchers would be able to use stable existing algorithms implementations when developing new ones.
My problem is that Idea doesn't recognize properly the Gradle structure of the second level Gradle sub project: Project is recognized well, Research and Development are recognized well, but the Algorithms sub project is not recognized:
The code inspection of the gradle.build file in Algorithms project is highlighted as a warning with a message that test cannot be applies to Groovy.lang.closure Note that below is the complete build.gradle:
test{
useTestNG()
testLogging.showStandardStreams = true
}
Also, when I try in Idea Run->Edit Configurations->Defaults->Gradle and trying to choose Gradle project, I see that in the pop-up menu Development and Research do appear as sub-projects of Project, but Algorithms appears as a stand alone project.
Making the project however does create all the binaries and manually running a test case also works well.
When I run Gradle for command line, it appears to be working well and recognizing all nested projects: All the code is being compiled and tests are being ran. So the question here is if there is an Idea issue here which is not working correctly with Gradle, or am I doing something wrong?
Code for build.gradle inside Project:
allprojects {
group 'com.project'
version 'v0.1'
repositories {
mavenCentral()
}
}
Code for settings.gradle inside Project
rootProject.name = 'Project'
include 'Development'
include 'Development:Algorithms'
include 'Research'
Code for build.gradle of Development sub-project:
subprojects {
apply plugin: 'java'
repositories {
jcenter()
}
dependencies{
testCompile 'org.testng:testng:6.9.4'
}
}
Other code samples that I might be writing wrong (most of them are auto generated by Idea:
Project.iml:
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="Project" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="com.project" external.system.module.version="v0.1" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build" />
<output-test url="file://$MODULE_DIR$/build" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Development.iml
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":Development" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.project" external.system.module.version="v0.1" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build" />
<output-test url="file://$MODULE_DIR$/build" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Algorithms.iml
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":Development:Algorithms" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../.." external.system.id="GRADLE" external.system.module.group="com.project" external.system.module.version="v0.1" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library>
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/plugins/testng/lib/testng.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
I would like to point the following:
All projects and sub-projects have the same group: com.project
In Algorithms.iml the original (auto generated) value of external.linked.project.id was :Algorithms. I have tried setting it to :Development:Algorithms to see if it will change anything. After each change I have ran rebuild.
In Algorithms.iml the original (auto generated) value of external.root.project.path was $MODULE_DIR$/... I have tried setting it to $MODULE_DIR$/../.. to see if it will change anything. After each change I have ran rebuild.
****************************Edit********************************
Further investigation shows an issue with the workspace.xml file:
There is a GradleLocalSettingsElement there that was automatically created, but appears to be wrong: There are two different entries: one for Development and Devlopment:Algorithms and onw for the rest, so I might have did something wrong when I created the project.
My question is if there is a reference for the structure of this file and specifically for the elements inserted by the Gradle plugin?
<map>
<entry>
<key>
<ExternalProjectPojo>
<option name="name" value="Development" />
<option name="path" value="$PROJECT_DIR$/Development" />
</ExternalProjectPojo>
</key>
<value>
<list>
<ExternalProjectPojo>
<option name="name" value="Development" />
<option name="path" value="$PROJECT_DIR$/Development" />
</ExternalProjectPojo>
<ExternalProjectPojo>
<option name="name" value=":Development:Algorithms" />
<option name="path" value="$PROJECT_DIR$/Development/Algorithms" />
</ExternalProjectPojo>
</list>
</value>
</entry>
<entry>
<key>
<ExternalProjectPojo>
<option name="name" value="Project" />
<option name="path" value="$PROJECT_DIR$" />
</ExternalProjectPojo>
</key>
<value>
<list>
<ExternalProjectPojo>
<option name="name" value="Project" />
<option name="path" value="$PROJECT_DIR$" />
</ExternalProjectPojo>
<ExternalProjectPojo>
<option name="name" value=":Development" />
<option name="path" value="$PROJECT_DIR$/Development" />
</ExternalProjectPojo>
<ExternalProjectPojo>
<option name="name" value=":Research" />
<option name="path" value="$PROJECT_DIR$/Research" />
</ExternalProjectPojo>
</list>
</value>
</entry>
</map>
a simpliest situation: I have a drools flow.
Very simple,like
"<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://drools.org/drools-5.0/process"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
type="RuleFlow" name="ruleflow" id="com.sample.ruleflow" package-name="com.sample" >
<header>
</header>
<nodes>
<start id="1" name="Start" x="16" y="16" width="48" height="48" />
<actionNode id="2" name="Hello" x="96" y="16" width="80" height="48" >
<action type="expression" dialect="mvel" >System.out.println("Hello World");</action>
</actionNode>
<end id="3" name="End" x="208" y="16" width="48" height="48" />
</nodes>
<connections>
<connection from="1" to="2" />
<connection from="2" to="3" />
</connections>
</process>"
How can I change it programmatically , not with loading the flow file to XML Parsers etc, but to add the nodes and connection through the code?
Thanks in advance
I don't think any Drools API will be there for modifying the rules