JaxB unmarshalling - create Reference to Object - java

Greetings fellow programmers!
I am having a major problem while unmarshalling xml to an object. I need to a create a reference to an object. How can I accomplish it? I've tried to use XSL transformation but due to cycle occurence this wasnt a right approach. I am 100% sure there's an easy way out.. Here's my XML:
<report>
<subject>
<subjectId>1</subjectId>
<name>John</name>
<surname>Doe</surname>
</subject>
<subject>
<subjectId>2</subjectId>
<name>Frank</name>
<surname>Timothy</surname>
</subject>
<individual>
<individualId>10</individualId>
<name>Isaac</name>
<surname>Newton</surname>
<co-worker>
<subject>
<subjectId>1</subjectId>
<inXml>true</inXml>
</subject>
<subject>
<subjectId>2</subjectId>
<inXml>true</inXml>
</subject>
</co-worker>
</individual>
<owner>
<subject>
<subjectId>2</subjectId>
<inXml>true</inXml>
</subject>
<share>100</share>
</owner>
</report>
I need to create reference of "subject" or "individual" with inXml=true. This flag tells me wether we have got entity with that ID in the XML.
Thank you very much for your help :)

You can use #XmlID/#XmlIDREF to map key based relationships in your XML document.
Example
http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html

Related

How to send request through SOAP UI with CDATA within CDATA to Java Application?

I am getting following exception while parsing XML
org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
SOAp Request i am sending:
<soap:Envelope>
<soap:Header/>
<soap:Body>
<ver:ReceiveOnlineBatchExternalAttachment>
<web:username>user</web:username>
<web:passwd>pass</web:passwd>
<web:receiversReference>1232323</web:receiversReference>
<web:sequenceNumber>1</web:sequenceNumber>
<web:batch>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<DataBatch>
<DataUnits>
<DataUnit>
<FormTask>
<ServiceCode>323015</ServiceCode>
<Form>
<FormData>
<![CDATA[<melding> </melding>]]
</FormData>
</Form>
</FormTask>
</DataUnit>
</DataUnits>
</DataBatch>
]]>>
</web:batch>
</ver:ReceiveOnlineBatchExternalAttachment>
</soap:Body>
</soap:Envelope>
I have done changes to CDATA multiple times but getting same error.
Could anyone suggest how CDATA within CDATA can be handled in this request?
Tried following but failed:
<![CDATA[ <elements> <![CDATA[<melding> </melding>]] <elements> ]]>>
<![CDATA[ <elements> <![CDATA[<melding> </melding>]]> <elements> ]]>
<![CDATA[ <elements> <![CDATA[<melding> </melding>]]> <elements> ]]>
I speculate that your problem is actually being caused by nested CDATA content. Can you try the following:
<![CDATA[
<DataBatch>
<DataUnits>
<DataUnit>
<FormTask>
<ServiceCode>323015</ServiceCode>
<Form>
<FormData>
<![CDATA[
<melding> </melding>
]]]]><![CDATA[>
</FormData>
</Form>
</FormTask>
</DataUnit>
</DataUnits>
</DataBatch>
]]>
The gist of the trick here is that when the parser hits the inner nested <![CDATA[ it will actually ignore it. Then, when it hits this:
]]]]><![CDATA[>
the first ]] will also be ignored, the following ]]> will be consumed, closing the outer CDATA, and then a new CDATA is immediately opened again which should last until the outer closing for that.
Edit by #fatherazrael:
Remove the XML tags <?xml version="1.0" encoding="UTF-8"?>
Reference: Nested CDATA - correctly

How are images handled in Microsoft Word

I am trying to embed images in xml document and save the document as a single document and be able to distribute the file just like microsoft word where both the text and images are saved in single docx file.
How is it done in Microsoft word. Do they use base64 encoding ?
It is quite hard to do it by hand and would look something like that:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:r>
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="5943600" cy="3717290"/>
<wp:docPr id="1" name="Picture 0" descr="vlcsnap-325726.png"/>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="0" name="myImage.png"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId4"/>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="5943600" cy="3717290"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
</w:p>
</w:body>
</w:document>
You don't want to do it that way. I would recommend to use an external library like OpenXML SDK 2.0 (by Microsoft) or OpenXML4J (especially for Java). They will make your life way easier

How to fetch a protion of xml in java?

My requirement is to fetch all the things in between particular tag in xml.
What are the possible ways of doing this?
for example my xml looks something like this:
<?xml version="1.0"?>
<class>
<teacher>
<subject>English</subject>
</teacher>
<student rollno="393">
<firstname>dinkar</firstname>
<lastname>kad</lastname>
<nickname>dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>jasvir</firstname>
<lastname>singh</lastname>
<nickname>jazz</nickname>
<marks>90</marks>
</student>
</class>
So how can I get the all the things in between <teacher> tag
seems like Xpath will be a good fit or you

how to save web-harvest data to database

I am scrapping the data using web-harvest tool and i am getting the required data i.e. name and price of the product.
here is my config file.
<include path="functions.xml"/>
<!-- collects all tables for individual products -->
<var-def name="products">
<call name="download-multipage-list">
<call-param name="pageUrl">http://www.amazon.de/s/ref=nb_sb_noss?__mk_de_DE=AMAZON&url=search-alias%3Daps&field-keywords=AT300-103%20TEGRA%203%201GB</call-param>
<call-param name="nextXPath">//a[#class="pagnNext"]/#href</call-param>
<call-param name="itemXPath">//div[#class="fstRow prod"]</call-param>
<call-param name="maxloops">10</call-param>
</call>
</var-def>
<!-- iterates over all collected products and extract desired data -->
<file action="write" path="reports/catalog.xml" charset="UTF-8">
<![CDATA[ <catalog> ]]>
<loop item="item" index="i">
<list><var name="products"/></list>
<body>
<xquery>
<xq-param name="item" type="node()"><var name="item"/></xq-param>
<xq-expression><![CDATA[
declare variable $item as node() external;
let $name := data($item//*[#class='lrg bold'])
let $price := data($item//*[#class='bld lrg red'])
return
<product>
<name>{normalize-space($name)}</name>
<price>{normalize-space($price)}</price>
</product>
]]></xq-expression>
</xquery>
</body>
</loop>
<![CDATA[ </catalog> ]]>
</file>
now i am trying to move this name and price information to the mysql database table which contains two columns name and price. i got the information that we have to use database tag. but not getting information how to use that.
could you please assist me how can configure that in my config file.
Thanks in advance.
Sahiti
Please go thorugh web-harvest.sourceforge.net/manual.php#database and try to implement as given.

Will a Single XSLT file solve this issue..or...?

Below is my XML File -
<CVs>
<CV>
<Name>ABC</Name>
<Address></Address>
<Introduction></Introduction>
<CompSkills>Java, XSLT, XPATH, XML, Oracle, VB.NET</CompSkills>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<CV>
<CV>
<Name>XYZ</Name>
<Address></Address>
<Introduction></Introduction>
<CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<CV>
</CVs>
below is the XSLT file - a short version to get an idea
<xsl:template match="Name">
<table align='center' width='800' style="font-family:tahoma; font-size:13pt;">
<tr><td>
<xsl:apply-templates/>
</td></tr>
</table>
</xsl:template>
<xsl:template match="Experience">
<table align='center' width='800' style="font-family:tahoma; font-size:13pt;">
<tr><td>
<xsl:apply-templates/>
</td></tr>
</table>
</xsl:template>
I am using Java as front-end. To display the output in HTML format I have an XSLT file. This XSLT file is a standard one ie; it displays all the CVs.
Now what I have to do is use a ListBox with Names of all candidates and when clicked on a particular name ONLY his CV should get displayed. I have coded the Java part to display the names of the candidates into the ListBox. Now have some trouble with displaying the CV of the selected candidate in HTML format.
The current XSLT file is displaying all the CVs. So Will I need another XSLT file which use parameter passed from the program and display its details..? If yes then some help on how to do this... ??
Thanks in advance - John
What you can do is add an xsl:param to your XSLT and give it a default value; 'All' for example. That way by default it will display all CVs.
If you need to display a single CV based on a Name, you can pass that value (from your ListBox) in your xsl:param to display only that CV.
Here's an example xsl:param and the xsl:template needed to display the CVs:
<xsl:param name="pName" select="'All'"/>
<xsl:template match="CV">
<xsl:if test="$pName = 'All' or Name = $pName">
<xsl:apply-templates/>
</xsl:if>
</xsl:template>
If you wanted to display only the XYZ CV, you would just use the value XYZ for the pName param when you called the XSLT.
To give you an idea how this can be done, here is a complete solution that extracts all or just the wanted CV element (no HTML formatting is done as this isn't relevant to the question):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pName" select="'XYZ'"/>
<xsl:template match="node()|#*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CV">
<xsl:if test="$pName = Name or $pName='*'">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied to the provided XML document (corrected to a well-formed one):
<CVs>
<CV>
<Name>ABC</Name>
<Address></Address>
<Introduction></Introduction>
<CompSkills>Java, XSLT, XPATH, XML, Oracle, VB.NET</CompSkills>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
</CV>
<CV>
<Name>XYZ</Name>
<Address></Address>
<Introduction></Introduction>
<CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
<Experience>
<Profile></Profile>
<Duration></Duration>
<Info></Info>
</Experience>
</CV>
</CVs>
the wanted, correct (just the CV with Name XYZ is extracted) is produced:
<CVs>
<CV>
<Name>XYZ</Name>
<Address/>
<Introduction/>
<CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills>
<Experience>
<Profile/>
<Duration/>
<Info/>
</Experience>
<Experience>
<Profile/>
<Duration/>
<Info/>
</Experience>
<Experience>
<Profile/>
<Duration/>
<Info/>
</Experience>
</CV>
</CVs>
Explanation:
The wanted name or "*" must be passed externally as a global parameter (in this case named pName) to the transformation. Read your XSLT processor documentation how this must be done, as this is implementation-dependent.

Categories