JAXB, save type of fields - java

How can I save types of class fields after marshalling without using of annotation. How can I edit it without annotations? What's the best way so it looks like in the example below?
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:o xmlns:ns2="info.source4code.jaxb.model">
<test>aaaa</test>
<intTest>100</intTest>
<abc>2</abc>
</ns2:o>
Wanted output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:o xmlns:ns2="info.source4code.jaxb.model">
<test type="String">aaaa</test>
<intTest type="Integer">100</intTest>
<abc type="Integer">2</abc>
</ns2:o>

Related

org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected

I have the following file that I cURL:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ProcessMessage xmlns="http://www.user.com/test/">
<envelope>Encoded XML request message will go here</envelope>
</ProcessMessage>
</soap:Body>
</soap:Envelope>
The full error I get is this:
org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
When I use the file with a CDATA element then everything works as expected.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ProcessMessage xmlns="http://www.user.com/test/">
<![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<envelope>Encoded XML request message will go here</envelope>
]]>
</ProcessMessage>
</soap:Body>
</soap:Envelope>
Any idea how I can accept an XML file without the CDATA part?
quote: "Only one XML declaration is permitted in well-formed XML, and it must be at the top if anywhere."
See: https://stackoverflow.com/a/20251895/3710053
So if you could get rid of that second <?xml version="1.0" encoding="UTF-8"?> your okay.

Multiple values to a single index

I stored the data from a csv in String[] but single index contains many values
For Example:- a[0] hasJohn Smith Jane Doe Jaan Lepp FooBar. So when I am parsing it to XML it creates 4 context for these 4 values. How can I pass all the name in single athletes node?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Athletes>
<Name>John Smith</Name>
<Athletes>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Athletes>
<Name>Jane Doe</Name>
<Athletes>

xml schema in jaxb java not getting expected output

My expected output is in this format of JAXB.
<?xml version="1.0" encoding="utf-8"?>
<req:ShipmentRequest xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" schemaVersion="5.0">
<Request>
<ServiceHeader>
<MessageTime>2001-12-17T09:30:47-05:00</MessageTime>
<MessageReference>1234567890123456789012345678901</MessageReference>
<SiteID>CustomerTest</SiteID>
<Password>alkd89nBV</Password>
</ServiceHeader>
</req:ShipmentRequest>
but i am able to get only
<?xml version="1.0" encoding="utf-8"?>
<ShipmentRequest xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" schemaVersion="5.0">
<Request>
<ServiceHeader>
<MessageTime>2001-12-17T09:30:47-05:00</MessageTime>
<MessageReference>1234567890123456789012345678901</MessageReference>
<SiteID>CustomerTest</SiteID>
<Password>alkd89nBV</Password>
</ServiceHeader>
</ShipmentRequest>
how can i add a prefix like "req:" as shown in the expected output as i am using jaxb to marshall and unmarshall

Find out wheter <?xml-stylesheet ?> node exists in xml-document

I've got a XML-document with this content:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="/Stylesheet.xsl" ?>
How can I find out using ANT's <xmltask> whether the <?xml-stylesheet ?> node already exists?
That's a special node type called processing instruction.
You can query it using XPath to find out if it exists and what its attributes are:
/processing-instruction('xml-stylesheet')

XSLT and Java: Default Namespace XMLNS

I'm new to XSLT and I'm trying to transform one XML file into another one. My problem is that there is one namespace 'xmlns' without any prefix in the original xml file and when I transform it to another one through xslt then nothing happens but if I remove that xmlns namespace then it works out but actually I cannot modify the original .xml file since I'm bound to use that file only, so I have to keep xmlns in the original file as it is. So can anyone please suggest some modifications in my .xsl or java code to overcome this problem.
My Original XML namespace looks like-
<?xml version="1.0" encoding="UTF-8"?>
<manifest identifier="eXeorm_sample4823c6301f29a89a4c1f"
xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
</manifest>
My desired xml is:
<?xml version="1.0" encoding="UTF-8"?>
<manifest identifier="eXescorm_quiz4823c6301f29a8419515"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
</manifest>
My Modified XSLT-
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="ims:manifest">
</xsl:stylesheet>
There's a search box at the top of this page: type "XSLT default namespace" and you will find hundreds of answers to this question.
Incidentally, your code is incredibly verbose. Instead of this:
<xsl:element name="item">
<xsl:attribute name="identifier">ITEM-eXeorm_sample4823c6301f29a89a4d27</xsl:attribute>
<xsl:attribute name="isvisible">true</xsl:attribute>
<xsl:attribute name="identifierref">RES-eXeorm_sample4823c6301f29a89a4d28</xsl:attribute>
</xsl:element>
use this:
<item identifier="ITEM-eXeorm_sample4823c6301f29a89a4d27" invisible="true"
identifierref="RES-eXeorm_sample4823c6301f29a89a4d28"/>

Categories