Java parsing XML with multiple arguments - java

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>

Related

IntelliJ Idea Not showing GUI

For some reason my GUI is no longer showing up on the .form file, instead it is all the hard code for it like:
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="emroGUI">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="11" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="742" height="400"/>
</constraints>
<properties/>
<border type="none"/>
etc.
When I originally made the GUI I did it via 'drag and drop' and I saw the actual GUI form. How do I get the actual form back?
your XML content malformed now and I think you are missing following ending tags:
</grid> </form>
accurate structure is as following:
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="emroGUI">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="11" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="742" height="400"/>
</constraints>
<properties/>
<border type="none"/>
</grid>
</form>

java form for master detail data entry using database

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>

Reflecting img.Xml into Java Objects Using JAXB

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.

java XML parsing using jaxb or dom?

I have a following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Abc version="3" xmlns="urn:Abc-v3">
<Ele1>
<SubElement name ="name" description="DO">
<Node version="C" siteURL="https://example.com" />
<Client>
<ClientId>1</ClientId>
<ClientSecret>Yes</ClientSecret>
</Client>
</SubElement>
<SubElement name ="SharePointOLDev1" description="DEV1">
<Node version="C" siteURL="https://example.com" />
<Local>
<LocalId>id</LocalId>
<Password>password</Password>
</Local>
</SubElement>
<SubElement name="AS" description="wow">
<DB connection="connection" />
</SubElement>
</Ele1>
<Ele2>
<Content ID="A" co="LD">
<Description>Simple Docs</Description>
<Serve
Lib1="TEST"
Lib2="yes"
Lib3="yes"
Lib4="no"
Lib5="no"
Lib6="name">
<Hole enabled="false" count="200" />
<Fol enabled="false" count="100" />
<Role enabled="No" validate="false" />
<FetchFilenameAttribute connection="SAP-AS" delay="3" />
</Serve>
</Content>
<Content ID="B" co="OL">
<Description>Simple Docs </Description>
<Serve
Lib1="TEST"
Lib2="yes"
Lib3="yes"
Lib4="no"
Lib5="no"
Lib6="name"">
<Hole enabled="false" count="200" />
<Fol enabled="false" count="100" />
<Role enabled="No" validate="false" />
</Serve>
</Content>
</Ele2>
<Ele3>
<CNode attr="hai" attr1="bye" />
</Ele3>
</Abc>
I need to parse this XML file and assign values to its corresponding class objects.Which is the best option to parse such an xml file.
JAXB sounds good to me but the POJOs were already written by someone and now i will have to rewrite and deploy them.ALso teh xml file has some errors while running xjc command.
DOM approach seems to be very cumbersome n error prone.
Please suggest.
PS:Kindly excuse my beginner level knowledge.
the JDK project comes with SAX(Simple API for XML) accessible by importing org.xml.sax.*.
You may take a look at this https://www.tutorialspoint.com/java_xml/java_sax_parse_document.htm for an introduction to the subject.

How can I change the predefined Drools Flow process programmatically?

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

Categories