java XML parsing using jaxb or dom? - java

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.

Related

Change value in XML file via Android Java

I want to change the "value" and "defaultValue" of the "drop" that contains "a33"
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<settings version="-1">
<setting id="1" name="a1" value="VAL555" drop="a12" defaultValue="VAL555" default="false" tag="null" />
<setting id="2" name="a2" value="VAL555" drop="a22" defaultValue="VAL555" default="false" tag="null" />
<setting id="3" name="a3" value="VAL555 <== I WANT TO CHANGE THIS" drop="a33" defaultValue="VAL555 <== I WANT TO CHANGE THIS" default="false" tag="null" />
<setting id="4" name="a4" value="VAL555" drop="a44" defaultValue="VAL555" default="false" tag="null" />
<setting id="5" name="a5" value="VAL555" drop="a55" defaultValue="VAL555" default="false" tag="null" />
</settings>
I want to do this for an app in Android Java
Currently, I can't go anywhere.
I have no idea where to start from, or where to look for.

IzPack Shortcut Nightmare

I'm at my wit's end with this.
I have a VERY simple installation descriptor for izpack for a two-package Java app on Windows. Everything works as intended EXCEPT for the shortcut creation. The shortcut panel doesn't seem right, for a start. The label "ShortcutPanel.regular.startup" is displayed instead of "run at startup" or anything of the sort in the startup execution checkbox.
After running the installer, the shortcuts simply aren't created. Here's what my install.xml looks like, it's pretty straightforward:
<izpack:installation version="5.0"
xmlns:izpack="http://izpack.org/schema/installation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://izpack.org/schema/installation http://izpack.org/schema/5.0/izpack-installation-5.0.xsd">
<info>
<appname>application_name</appname>
<appversion>2.0.0</appversion>
<appsubpath>myapp</appsubpath>
<javaversion>1.8</javaversion>
</info>
<locale>
<langpack iso3="bra" />
</locale>
<resources>
<res src="shortcutSpec.xml" id="shortcutSpec.xml"/>
</resources>
<variables>
<variable name="TargetPanel.dir.windows" value="C:/Sistemas"/>
</variables>
<guiprefs width="800" height="600" resizable="no">
<laf name="substance">
<os family="windows" />
<os family="unix" />
<param name="variant" value="mist-silver" />
</laf>
<modifier key="useHeadingPanel" value="yes" />
</guiprefs>
<panels>
<panel classname="HelloPanel" />
<panel classname="DefaultTargetPanel" />
<panel classname="ShortcutPanel" />
<panel classname="InstallPanel" />
<panel classname="FinishPanel" />
</panels>
<natives>
<native type="izpack" name="ShellLink.dll">
<os family="windows"/>
</native>
<native type="izpack" name="ShellLink_x64.dll">
<os family="windows"/>
</native>
</natives>
<packs>
<pack name="Pack1" required="yes">
<description>one of the packs it's a jar</description>
<file src="lib/pack1.jar" targetdir="$INSTALL_PATH/pack1subdir"
override="true">
</file>
<file src="imagens/logo.ico" targetdir="$INSTALL_PATH/pack1subdir/imagens/"
override="true" />
<executable targetfile="lib/pack1.jar" type="jar"
stage="never"></executable>
</pack>
<pack name="Pack2" required="yes">
<description>the other pack</description>
<file src="lib/pack2.jar" targetdir="$INSTALL_PATH/pack2subdir"
override="true">
</file>
<file src="imagens/update.ico" targetdir="$INSTALL_PATH/pack2subdir/imagens/"
override="true" />
<executable targetfile="lib/pack2.jar" type="jar"
stage="never"></executable>
</pack>
</packs>
Then there's the shortcutSpec.xml. My pom moves it to my staging directory:
<izpack:shortcuts version="5.0"
xmlns:izpack="http://izpack.org/schema/shortcuts" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://izpack.org/schema/shortcuts http://izpack.org/schema/5.0/izpack-shortcuts-5.0.xsd">
<lateShortcutInstall />
<shortcut name="pack1" programGroup="false"
desktop="true" applications="false" startMenu="no" startup="true"
target="java -jar $INSTALL_PATH/pack1subdir/pack1.jar"
workingDirectory="$INSTALL_PATH/pack1subdir/">
<createForPack name="pack1" />
</shortcut>
<shortcut name="pack2" programGroup="false"
desktop="true" applications="false" startMenu="no" startup="true"
target="java -jar $INSTALL_PATH/pack2subdir/pack1.jar"
workingDirectory="$INSTALL_PATH/pack2subdir/">
<createForPack name="pack2" />
</shortcut>
The installer simply doesn't create any shortcuts anywhere. It finishes with no error messages no logs no stack traces no nothing.
Help.
I fixed that by going into the izPack/lib/izpack-core-5.0.9.jar and opening the /com/izforge/izpack/bin/langpacks/installer/ folder in it. Then editing the bra.xml file and adding the following line:
<str id="ShortcutPanel.regular.startup" txt="Iniciar com o Windows"/>
Then save and update the file in the jar and rebuild your instalation.

How to add Annotations elements in metadata generated by Apache Olingo V2.0?

I have developed Odata service for a system entity which generates a metadata but however I cant figure out how to add Annotations element to it. Sample Metadata generated is as follows :-
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:sap="http://www.sap.com/Protocols/SAPData" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
m:DataServiceVersion="1.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="myNamespace" sap:schema-version="1">
<EntityType Name="System">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="name" Type="Edm.String" sap:label="System Name" sap:creatable="false"
sap:updatable="false" sap:sortable="false" sap:required-in-filter="true"/>
<Property Name="description" Type="Edm.String" />
<Property Name="status" Type="Edm.String" />
<Property Name="type" Type="Edm.String" />
</EntityType>
<EntityContainer Name="ODataEntityContainer" m:IsDefaultEntityContainer="true">
<EntitySet Name="Systems" EntityType="myNamespace.System" />
<FunctionImport Name="NumberOfSystems" ReturnType="Collection(myNamespace.System)"
m:HttpMethod="GET" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
I need to add following elements to above metatada
<Annotations Target="myNamespace.System"
xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Annotation Term="com.sap.vocabularies.UI.v1.LineItem">
<Collection>
<Record Type="com.sap.vocabularies.UI.v1.DataField">
<PropertyValue Property="Value" Path="name" />
</Record>
<Record Type="com.sap.vocabularies.UI.v1.DataField">
<PropertyValue Property="Value" Path="description"/>
</Record>
<Record Type="com.sap.vocabularies.UI.v1.DataField">
<PropertyValue Property="Value" Path="status" />
</Record>
</Collection>
</Annotation>
</Annotations>
I came across the org.apache.olingo.commons.api.edm.provider.annotation package but cant find any suitable API. Please let me know how should I proceed.
Thanks in advance.
The annotations you would like to use have been introduced with OData V3 which is why they are not directly supported with the Olingo V2 library.
You can use the EdmProvider AnnotationElement and AnnotationAttribute classes to mimic this behaviour though. For example You can create a AnnotationElement with the name "Annotations" this element will then have the "AnnotationAttribute" Target=SomeString. Since an "AnnotationElement" can have child elements you can put your Collection element there. Namespaces are also handled with "AnnotationAttributes".
You can only attach the annotation to Edm elements which are derived from the EdmAnnotatable interface. So this is a difference to V3.
This is currently the only way to get this behaviour with Olingo V2.

Multinesting Gradle Projects in IntelliJ Idea

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>

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