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.
Related
I'm trying to create a relation between Product2Componen
<relation code="Product2Components" localized="false" autocreate="true">
<deployment table="ProdsCompRels" typecode="30008" />
<sourceElement qualifier="product" type="Product" cardinality="one" collectiontype="list">
<modifiers read="true" write="true" search="true" optional="true" />
</sourceElement>
<targetElement qualifier="components" type="SimpleCMSComponent" cardinality="many">
<modifiers read="true" write="true" search="true" optional="true" />
</targetElement>
</relation>
However , hybris OOTB has already a relation within product2ProductDetailComponent:
<relation code="ProductDetailComponentsForProduct" generate="true" localized="false" autocreate="true">
<sourceElement qualifier="productDetailComponents" type="ProductDetailComponent" cardinality="many" collectiontype="list">
<modifiers read="true" write="true" search="true" optional="true" />
</sourceElement>
<targetElement qualifier="product" type="Product" cardinality="one">
<modifiers read="true" write="true" search="true" optional="true" />
</targetElement>
</relation>
And when I compile it returns me this error because of this already existing relation that is completly useless to me:
Attribute CmsLinkComponent.product(Product):((cms2))::YAttributeDescriptor[cms2-items.xml:190(RelationTypeTagListener)][JALO] duplicates inherited attribute SimpleCMSComponent.product(Product):((puigcore))::YAttributeDescriptor[puigcore-items.xml:488(RelationTypeTagListener)][JALO]. Remove it or specify it as redeclared
How can I redeclare a relation ? Or what should I do here ?
Since the OOTB ProductDetailComponent extends SimpleCMSComponent and you want to use SimpleCMSComponent so I think you can simply use OOTB relation.
But if you really want to decare your own attribute/relation then you have to provide a different qualifier other than "product", as the product qualifier is already been used in the OOTB relation ProductDetailComponentsForProduct, where ProductDetailComponent is the subtype of SimpleCMSComponent.
<relation code="Product2Components" localized="false" autocreate="true">
<deployment table="ProdsCompRels" typecode="30008" />
<sourceElement qualifier="productRef" type="Product" cardinality="one" collectiontype="list">
<modifiers read="true" write="true" search="true" optional="true" />
</sourceElement>
<targetElement qualifier="components" type="SimpleCMSComponent" cardinality="many">
<modifiers read="true" write="true" search="true" optional="true" />
</targetElement>
</relation>
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.
This is a sample of the XML I've got. It contains several more rows with item
<items>
<item name="Alfa Romeo">
<models>
<model to="2001" since="1994" name="145/146" />
<model since="2001" name="147" />
<model to="1997" since="1992" name="155" />
<model since="1997" name="156" />
<model since="2007" name="159" />
<model to="1998" since="1990" name="164" />
<model since="1999" name="166" />
<model since="2004" name="GT" />
<model since="1996" name="GTV" />
<model to="2007" since="1996" name="Spider" />
<model since="2007" name="Spider" />
</models>
</item>
<item name="Fiat">
<models>
<model since="1995" name="Barchetta" />
<model since="2007" name="Bravo" />
<model since="1995" name="Bravo and Brava" />
<model to="1998" since="1993" name="Cinquecento" />
<model to="2000" since="1995" name="Coupe" />
<model since="2001" name="Doblo" />
<model since="2007" name="Grande Punto" />
<model to="2007" since="2004" name="Idea" />
<model to="2001" since="1997" name="Marea" />
<model since="1999" name="Multipla" />
<model since="2004" name="Panda" />
<model to="2003" since="1994" name="Punto" />
<model since="2003" name="Punto" />
<model to="2004" since="1998" name="Seicento" />
<model since="2002" name="Stilo" />
<model to="1996" since="1991" name="Tempra" />
<model to="1996" since="1989" name="Tipo" />
<model to="2003" since="1995" name="Ulysse" />
</models>
</item>
</items>
Is it posible to unmarshall this into objects of Model with fields private int to; private int since; private String name; contained in an ArrayList inside models of Car(item) contained in an ArrayList?
Alternatively with objects of Car with fields private String brand(name); private int since; private int to; private String model(name) contained in an ArrayList?
If so how? or if not how other way can I retrieve the information into objects and a data structure?
I've started going through the very basic tutorials to learning JAXB, so I'm getting there step by step, but I would really like to be able to use this XML data in another java project I'm working on as soon as possible.
Thanks.
You should start by generating you POJO's. You can use an online tool to do this if you haven't done this already, see How to generate JAXB classes from just XML
I'm not sure what you're trying to accomplish with "private" explanation. First step, get it to unmarshall. You can then change decorations to make appropriate private fields - see How to marshal/unmarshal Java objects with private fields using JAXB
Essentially, use #XmlElement on private members:
public class model {
#XmlAttribute
private String to;
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();
}
I was creating custom content model
in datalistModel.xml
<type name="dl:car">
<title>Car List</title>
<parent>dl:dataListItem</parent>
<properties>
<property name="dl:carName">
<title>Car Name</title>
<type>d:text</type>
<mandatory>false</mandatory>
</property>
<property name="dl:carCompany">
<title>Company Name</title>
<type>d:text</type>
<mandatory>false</mandatory>
</property>
</properties>
</type>
in share-datalist-form-config.xml
<!-- dl:contact type create car form -->
<config evaluator="model-type" condition="dl:car">
<forms>
<!-- Create item form -->
<form>
<field-visibility>
<!-- dl:contact type -->
<show id="dl:carName" />
<show id="dl:carCompany" />
</field-visibility>
<create-form template="../data-lists/forms/dataitem.ftl" />
</form>
</forms>
</config>
When I create a new car content in repository browser(I later configured menu to add content type 'Car'), the file name is 91b65385-86c6-4923-859d-6ecb3326319c.
<create-content>
<content id="plain-text" mimetype="text/plain" label="create-content.text" itemid="cm:content" icon="text"/>
<content id="html" mimetype="text/html" label="create-content.html" itemid="cm:content"/>
<content id="xml" mimetype="text/xml" label="create-content.xml" itemid="cm:content"/>
<content id="car" mimetype="text/xml" icon="xml" label="create-content.car" itemid="dl:car"/>
</create-content>
How can I make the file name to carName instead of 91b65385-86c6-4923-859d-6ecb3326319c
Any kind help is appreciated.
Take a look at the default 'share-datalist-form-config.xml'
You will see that almost every type specify <show id="cm:title" />
You could also just put <show id="name" /> or <show id="cm:name" /> to show the name attribute.
In order to put the title you will need to put the cm:titled aspect in your model.
Just check the 'datalistModel.xml' how the defaults are defined
And check this blog post to know a bit more how to create custom datalists.