SimpleFramework Xml parsen - java

We have to Parse a XML String
<GetTimetableEntriesResponse Result="Success">
<TimetableEntries>
<TimetableEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" order="1" TimetableEntryId="7645" TimetableEntryGroupId="1739" Name="Alm_04.jpg" Duration="7000">
<Template xsi:type="XmlImageTemplate" duration="7000">
<VerticalScroll>false</VerticalScroll>
<HorizontalScroll>false</HorizontalScroll>
<ScrollSpeed>50</ScrollSpeed>
<ScrollSpeedBack>100</ScrollSpeedBack>
<ScrollStartDelay>0</ScrollStartDelay>
<MediaItem id="6629" x="0" y="0" zIndex="0" type="jpg">
<width xsi:nil="true" />
<height xsi:nil="true" />
<rotation xsi:nil="true" />
<rotationcenterx xsi:nil="true" />
<rotationcentery xsi:nil="true" />
<alpha xsi:nil="true" />
<VerticalScroll xsi:nil="true" />
<HorizontalScroll xsi:nil="true" />
<ScrollSpeed xsi:nil="true" />
<ScrollSpeedBack xsi:nil="true" />
<ScrollStartDelay xsi:nil="true" />
<Loop xsi:nil="true" />
</MediaItem>
<Effect xsi:type="XmlFade" start="0" from="0" to="1" duration="500" />
<Effect xsi:type="XmlFade" start="6500" from="1" to="0" duration="500" />
<TemplateSource xsi:nil="true" />
</Template>
</TimetableEntry>
</GetTimetableEntriesResponse>
but <TimetableEntry> got no <Template> directly in it, programmatically it is handled in a Subclass called <TimetableData> which is in <TimetableEntry>
That means we handle it in java like :
<TimetableEntry>
<TimetableData>
<Template></Template>
</TimetableData>
</TimetableEntry>
We don't know how we should parse it in our Objects.

Related

XML remove all child node from a node and create new one

I have this xml file:
<achievements>
<achievement id="1" name="Monster Slayer Lv1" description="Slay 15 monsters of any type" icon="Icon.skill0496" categoryId="1">
<conditions>
<condition name="Level" val="50" />
</conditions>
<items>
<item id="6393" count="1" />
</items>
</achievement>
<achievement id="2" name="Monster Slayer Lv2" description="Slay 50 monsters of any type" icon="Icon.skill0497" categoryId="1">
<conditions>
<condition name="MonsterKill" val="50" />
</conditions>
<items>
<item id="57" count="50000000" />
</items>
</achievement>
</achievements>
And i want my code:
try
{
final File inputFile = new File("C:/Users/ProjectX/Desktop/achievement.xml");
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
final Document doc = dBuilder.parse(inputFile);
for (Node node = doc.getFirstChild().getFirstChild(); node != null; node = node.getNextSibling())
{
if (node.getNodeName().equalsIgnoreCase("achievements"))
{
for (Node ach_node = node.getFirstChild().getFirstChild(); ach_node != null; ach_node = ach_node.getNextSibling())
{
if (ach_node.getNodeName().equalsIgnoreCase("achievement"))
{
}
}
}
}
}
catch (final Exception e)
{
e.printStackTrace();
}
to read until achievement and delete all sub-node (which include all condition and items or any other child node it might have). In addition i want to replace with my own node such as"
<dropList id="x">
<itemId="x" min="x" max="x" chance="x" />
</dropList>
Anyone know how to do this? Thanks in advance a lot to all community member who spend their time reading this.
<achievements>
<achievement id="1" name="Monster Slayer Lv1" description="Slay 15 monsters of any type" icon="Icon.skill0496" categoryId="1">
<conditions>
<condition name="Level" val="50" />
</conditions>
<dropList id="1">
<itemId="x" min="x" max="x" chance="x" />
</dropList>
<dropList id="2">
<itemId="x" min="x" max="x" chance="x" />
</dropList>
</achievement>
<achievement id="2" name="Monster Slayer Lv2" description="Slay 50 monsters of any type" icon="Icon.skill0497" categoryId="1">
<conditions>
<condition name="MonsterKill" val="50" />
</conditions>
<dropList id="1">
<itemId="x" min="x" max="x" chance="x" />
</dropList>
<dropList id="2">
<itemId="x" min="x" max="x" chance="x" />
</dropList>
</achievement>
</achievements>
It is better to use an XSLT transformation for your case. It has a specific pattern for exactly what you need. It is called Identity Transform.
All you need to do is just to run the XSLT below in your Java code.
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="items">
<dropList id="1">
<item Id="x" min="x" max="x" chance="x"/>
</dropList>
<dropList id="2">
<item Id="x" min="x" max="x" chance="x"/>
</dropList>
</xsl:template>
</xsl:stylesheet>
Output
<achievements>
<achievement id="1" name="Monster Slayer Lv1" description="Slay 15 monsters of any type" icon="Icon.skill0496" categoryId="1">
<conditions>
<condition name="Level" val="50"/>
</conditions>
<dropList id="1">
<item Id="x" min="x" max="x" chance="x"/>
</dropList>
<dropList id="2">
<item Id="x" min="x" max="x" chance="x"/>
</dropList>
</achievement>
<achievement id="2" name="Monster Slayer Lv2" description="Slay 50 monsters of any type" icon="Icon.skill0497" categoryId="1">
<conditions>
<condition name="MonsterKill" val="50"/>
</conditions>
<dropList id="1">
<item Id="x" min="x" max="x" chance="x"/>
</dropList>
<dropList id="2">
<item Id="x" min="x" max="x" chance="x"/>
</dropList>
</achievement>
</achievements>

Add an node to an xml when the same node is present in the xml as an empty tag

<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE>
<STATUS>
<SERVERTIME><![CDATA[15319123123262]]></SERVERTIME>
<TYPE><![CDATA[getData]]></TYPE>
<RESPONSE_VERSION><![CDATA[1]]></RESPONSE_VERSION>
<DATA_VERSION><![CDATA[1]]></DATA_VERSION>
<VALUE><![CDATA[1]]></VALUE>
<ERRORS />
<WARNINGS />
</STATUS>
<DATA>
<CONTENT RECORDID="f2e110aa8ca24aewq929d26b9fcf3108962">
<METARESOURCEPATH><![CDATA[sites/xyz/content/meta/INFORMATION/0/INF427/]]></METARESOURCEPATH>
<REVIEW_TIMESTAMP><![CDATA[2018-07-20 15:36:00 IST]]></REVIEW_TIMESTAMP>
<REVIEW_TIMESTAMP_MILLIS><![CDATA[1532081160000]]></REVIEW_TIMESTAMP_MILLIS>
<VIEWS>
<VIEW>
<NAME><![CDATA[xyz]]></NAME>
<REFERENCE_KEY><![CDATA[xyz]]></REFERENCE_KEY>
<GUID><![CDATA[6d024478feb441231661f163de62d6e]]></GUID>
<OBJECTID><![CDATA[001]]></OBJECTID>
<PARENTID />
<CHILDCOUNT><![CDATA[8]]></CHILDCOUNT>
</VIEW>
</VIEWS>
<CATEGORIES />
</CONTENT>
</DATA>
</RESPONSE>
I need to add an element to CATEGORIES WHICH IS OF THE FORMAT.
When ever i try to add a new category it does not accept it because it finds a duplicate categories tag. But when i try to remove that empty tag , that is also not accepted because getelementbyname only returns null for categories.
<CATEGORIES>
<CATEGORY>
<NAME><![CDATA[xyz]]></NAME>
<REFERENCE_KEY><![CDATA[xyz]]></REFERENCE_KEY>
<GUID><![CDATA[6d024478feb441231661f163de62d6e]]></GUID>
<OBJECTID><![CDATA[001]]></OBJECTID>
<PARENTID />
<CHILDCOUNT><![CDATA[8]]></CHILDCOUNT>
</CATEGORY>
</CATEGORIES>
How can this be done with max efficiency ?

Parsing xml with many attributes using Java JAXB

I have following xml which I am trying to parse using JAXB.
<?xml version="1.0" encoding="UTF-8"?>
<DataSet Name="core_donor" Version="2-0" SchemaVersion="1-0" SchemaDate="2014-01-16">
<DataPoints>
<DataPoint Name="donor_id" Value="105272" />
<DataPoint Name="surname" Value="TWO" />
<DataPoint Name="forename" Value="SCENARIO" />
<DataSubSet Name="blood_gas">
<DataPoints>
<DataPoint Name="blood_gas_no" Value="1" />
<DataPoint Name="blood_gas_date" Value="07/10/2014 10:15" />
</DataPoints>
<DataPoints>
<DataPoint Name="blood_gas_no" Value="2" />
<DataPoint Name="blood_gas_date" Value="07/10/2014 11:20" />
</DataPoints>
</DataSubSet>
<DataSubSet Name="liver_function">
<DataPoints>
<DataPoint Name="liver_function_no" Value="1" />
<DataPoint Name="liver_function_sample_date" Value="07/10/2014 11:10" />
</DataPoints>
<DataPoints>
<DataPoint Name="liver_function_no" Value="2" />
<DataPoint Name="liver_function_sample_date" Value="07/10/2014 13:52" />
</DataPoints>
</DataSubSet>
</DataPoints>
</DataSet>
I would be thankful if someone could point me how to achieve the parsing in java using JAXB.
Thanks!
For starters follow the below example
http://www.mkyong.com/java/jaxb-hello-world-example/
But DataPoints will be your RootElement , if am not wrong also refererence
check some examples for DOM parsing to traverse over the child nodes and read them.

What does "The prefix "vm" for element "vm:endpoint" is not bound." means and how to solve it?

I am trying to integrate mule with drools, I have mainly followed the tutorial here but I am getting this error :
"The prefix "vm" for element "vm:endpoint" is not bound."
can anyone help me solve this problem?
following is my .drl file and mule flow files.
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/bpm http://www.mulesoft.org/schema/mule/bpm/3.6/mule-bpm.xsd">
<http:listener-config name="HTTP_Listener_Configuration"
host="localhost" port="8084" doc:name="HTTP Listener Configuration" />
<spring:beans>
<spring:bean id="dto" class="com.hamgam.hit.esb.XMLDTO" scope="singleton" />
<spring:bean id="dto-convertor" class="com.hamgam.hit.esb.XMLToDTO" scope="singleton" />
<spring:bean id="rule-convertor" class="com.hamgam.hit.esb.RuleXMLToDRL" scope="singleton" />
</spring:beans>
<bpm:drools />
<flow name="basic_tutorialFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/{name}" doc:name="HTTP" />
<vm:endpoint name = "emc" path="drool.msg" />
<set-variable variableName="name" value="#[message.inboundProperties['http.uri.params']['name']]" />
<set-variable variableName="msg" value="#[payload]"/>
<logger level="DEBUG" doc:name="Logger" message="Current path is flowVars['name']" />
<expression-component>
app.registry['dto'] = app.registry['dto-convertor'].convert(flowVars['name']);
</expression-component>
<bpm:rules rulesDefinition="rule.drl" cepMode="true" initialFacts-ref="dto" entryPoint="XMLDTO stream" />
<choice doc:name="Choice">
<when expression="#[flowVars.emc == 'KeyValueMatched']">
<set-payload doc:name="Set Payload" value="#['KeyValueMatched! You are with us on 8084. ' + flowVars['name'] + '. Today is ' + server.dateTime.format('dd/MM/yy') + '.' ]" />
</when>
<otherwise>
<set-payload doc:name="Set Payload" value="#['Nothing! You are with us on 8084. ' + flowVars['name'] + '. Today is ' + server.dateTime.format('dd/MM/yy') + '.' ]" />
</otherwise>
</choice>
</flow>
</mule>
and the drl file :
global org.mule.module.bpm.MessageService mule;
import com.me.mit.esb.*;
rule "La Vita Ebela"
dialect "mvel"
when
$x:XMLDTO( inputXML == "inputXML" )
then
mule.generateMessage("emc", "KeyValueMatched", null, MessageExchangePattern.ONE_WAY);
end
You have to add XML namespache and scema reference to your xml file:
XML namespace:
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
XML Schema Location:
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.6/mule-vm.xsd
So it looks like so:
<mule
xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/bpm http://www.mulesoft.org/schema/mule/bpm/3.6/mule-bpm.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.6/mule-vm.xsd
">
Replase vm:endpoint by vm:inbound-endpoint. As Jens said, add vm schema to your mule-config. And don't forget to include mule-transports-vm-3.6.0.jar to your test classpath.

EDI file generation using SMOOKS

How can I convert XML file to EDI file using SMOOKS?
I am able to convert EDI to XML, in fact this is part of examples provided by SMOOKS.
Based on your question i tried to do some research on it . Please check if it helps you .
So here's the source edi file that is to be transformed:
HDR*1*0*59.97*64.92*4.95*Wed Nov 15 13:45:28 EST 2006
CUS*user1*Harry^Fletcher*SD
ORD*1*1*364*The 40-Year-Old Virgin*29.98
ORD*2*1*299*Pulp Fiction*29.99
And this is the expected result of our transformation:
<Order>
<header>
<order-id>1</order-id>
<status-code>0</status-code>
<net-amount>59.97</net-amount>
<total-amount>64.92</total-amount>
<tax>4.95</tax>
<date>Wed Nov 15 13:45:28 EST 2006</date>
</header>
<customer-details>
<username>user1</username>
<name>
<firstname>Harry</firstname>
<lastname>Fletcher</lastname>
</name>
<state>SD</state>
</customer-details>
<order-item>
<position>1</position>
<quantity>1</quantity>
<product-id>364</product-id>
<title>The 40-Year-Old Virgin</title>
<price>29.98</price>
</order-item>
<order-item>
<position>2</position>
<quantity>1</quantity>
<product-id>299</product-id>
<title>Pulp Fiction</title>
<price>29.99</price>
</order-item>
</Order>
The Smooks Configuration
We simply specify the SmooksEDIParser  as the stream parser. More transformation configurations could be added to transform this message further.
Here's the configuration ("smooks-config.xml"):
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:edi="http://www.milyn.org/xsd/smooks/edi-1.1.xsd">
<!--
Configure the EDI Reader to process the message stream into a stream of SAX events.
-->
<edi:reader mappingModel="/example/edi-to-xml-order-mapping.xml" />
</smooks-resource-list>
Here's the edi mapping ("/src/main/java/example/edi-to-xml-order-mapping.xml"):
<?xml version="1.0" encoding="UTF-8"?>
<medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.0.xsd">
<medi:description name="DVD Order" version="1.0" />
<medi:delimiters segment="
" field="*" component="^" sub-component="~" />
<medi:segments xmltag="Order">
<medi:segment segcode="HDR" xmltag="header">
<medi:field xmltag="order-id" />
<medi:field xmltag="status-code" />
<medi:field xmltag="net-amount" />
<medi:field xmltag="total-amount" />
<medi:field xmltag="tax" />
<medi:field xmltag="date" />
</medi:segment>
<medi:segment segcode="CUS" xmltag="customer-details">
<medi:field xmltag="username" />
<medi:field xmltag="name">
<medi:component xmltag="firstname" />
<medi:component xmltag="lastname" />
</medi:field>
<medi:field xmltag="state" />
</medi:segment>
<medi:segment segcode="ORD" xmltag="order-item" maxOccurs="-1">
<medi:field xmltag="position" />
<medi:field xmltag="quantity" />
<medi:field xmltag="product-id" />
<medi:field xmltag="title" />
<medi:field xmltag="price" />
</medi:segment>
</medi:segments>
</medi:edimap>
Executing The Transformation:
// Instantiate Smooks with the config...
Smooks smooks = new Smooks("smooks-config.xml");
try {
// Filter the input message to the outputWriter...
smooks.filterSource(new StreamSource(messageIn), new
StreamResult(messageOut));
} finally {
smooks.close();
}

Categories

Resources