How to unmarshal an XML String contain diffgr to JAVA POJO - java

Hi I got a below XML string which contain diffgram
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Result xmlns="">
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<DT1 diffgr:id="DT11" msdata:rowOrder="0">
<TEST_NO>93481324345</TEST_NO>
<TEST_DESC>Description1</TEST_DESC>
</DT1>
<DT1 diffgr:id="DT12" msdata:rowOrder="1">
<TEST_NO>93481366454</TEST_NO>
<TEST_DESC>Description2</TEST_DESC>
</DT1>
<DT1 diffgr:id="DT13" msdata:rowOrder="2">
<TEST_NO>934813267783</TEST_NO>
<TEST_DESC>Description3</TEST_DESC>
</DT1>
</NewDataSet>
</diffgr:diffgram>
</Result>
How can I unmarshal this String to java POJO using JAXB ?
Below are the current POJO seems not able to bind by this way.
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"diffgr",
"newDataSet",
"dt"
})
#XmlRootElement(name="Result")
public class Response {
#XmlElement(name = "diffgr:diffgram")
protected String diffgr;
#XmlElement(name = "NewDataSet")
protected String newDataSet;
#XmlElement(name = "DT1")
protected List<Response.DT> dt;
public String getDiffgr() {
return diffgr;
}
public void setDiffgr(String diffgr) {
this.diffgr = diffgr;
}
public String getNewDataSet() {
return newDataSet;
}
public void setNewDataSet(String newDataSet) {
this.newDataSet = newDataSet;
}
public List<Response.DT> getDt() {
if (dt == null) {
dt = new ArrayList<Response.DT>();
}
return this.dt;
}
public void setDt(List<Response.DT> dt) {
this.dt = dt;
}
#Override
public String toString() {
return "Response [diffgr=" + diffgr + ", newDataSet=" + newDataSet + ", dt=" + dt + "]";
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"testNo",
"testDesc"
})
public static class DT {
#XmlElement(name = "TEST_NO")
protected String testNo;
#XmlElement(name = "TEST_DESC")
protected String testDesc;
public String getTestNo() {
return testNo;
}
public void setTestNo(String testNo) {
this.testNo = testNo;
}
public String getTestDesc() {
return testDesc;
}
public void setTestDesc(String testDesc) {
this.testDesc = testDesc;
}
#Override
public String toString() {
return "DT [testNo=" + testNo + ", testDesc=" + testDesc + "]";
}
}
}
I'm using the below sample code to unmarshal the String
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller jaxbUnMarshaller = jaxbContext.createUnmarshaller();
Response response = (Response)jaxbUnMarshaller.unmarshal(new StringReader(xml));
When execute there is no any error throwing out while the POJO was empty.
How to unmarshal an XML String contain diffgr to JAVA POJO like this case?

Related

Parsing SOAP response to JAVA object

I am trying to parsing a Soap response string to a JAVA object to get those parameters.
Here's the response string:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns0:submitCdus1Response xmlns:ns0="http://example.com/">
<MessageId>D421425215</MessageId>
<NoOfDay>14</NoOfDay>
<Status>Y</Status>
<LastControlPoint>SBC</LastControlPoint>
<LastEntryDate>20210415</LastEntryDate>
<ReplyDateTime>20210427114126848</ReplyDateTime>
<TypeOfTravel>A</TypeOfTravel>
</ns0:submitCdus1Response>
</S:Body>
</S:Envelope>
My Object class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name="submitCdus1Response", namespace="http://example.com/" )
public class TravelHistoryResponseDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#XmlAttribute(name = "MessageId")
private String MessageId;
#XmlAttribute(name = "NoOfDay")
private Integer NoOfDay;
#XmlAttribute(name = "Status")
private String Status; //Y , N ,
#XmlAttribute(name = "LastControlPoint")
private String LastControlPoint;
#XmlAttribute(name = "TypeOfTravel")
private String TypeOfTravel;// A
#XmlAttribute(name = "LastEntryDate")
private Date LastEntryDate;
#XmlAttribute(name = "ReplyDateTime")
private Date ReplyDateTime;
public String getMessageId() {
return MessageId;
}
public void setMessageId(String messageId) {
MessageId = messageId;
}
public Integer getNoOfDay() {
return NoOfDay;
}
public void setNoOfDay(Integer noOfDay) {
NoOfDay = noOfDay;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public String getLastControlPoint() {
return LastControlPoint;
}
public void setLastControlPoint(String lastControlPoint) {
LastControlPoint = lastControlPoint;
}
public String getTypeOfTravel() {
return TypeOfTravel;
}
public void setTypeOfTravel(String typeOfTravel) {
TypeOfTravel = typeOfTravel;
}
public Date getLastEntryDate() {
return LastEntryDate;
}
public void setLastEntryDate(Date lastEntryDate) {
LastEntryDate = lastEntryDate;
}
public Date getReplyDateTime() {
return ReplyDateTime;
}
public void setReplyDateTime(Date replyDateTime) {
ReplyDateTime = replyDateTime;
}
#Override
public String toString() {
return "TravelHistoryResponseDTO [MessageId=" + MessageId + ", NoOfDay=" + NoOfDay + ", Status=" + Status
+ ", LastControlPoint=" + LastControlPoint + ", TypeOfTravel=" + TypeOfTravel + ", LastEntryDate="
+ LastEntryDate + ", ReplyDateTime=" + ReplyDateTime + "]";
}
}
My code:
SOAPMessage message = MessageFactory.newInstance().createMessage(null,
new ByteArrayInputStream(responseString.getBytes()));
Unmarshaller unmarshaller = JAXBContext.newInstance(TravelHistoryResponseDTO.class).createUnmarshaller();
TravelHistoryResponseDTO dto = (TravelHistoryResponseDTO)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
But I am getting
javax.xml.bind.UnmarshalException: unexpected element (uri:http://example.com/", local:"submitCdus1Response"). Expected elements are <{}submitCdus1Response>
Anyway to solve this and able to map the parameters to the java object?
There is an error in the JAXB object mapping : you are using #XmlAttribute for the various fields of your object, but the XML data you are trying to read are not XML attributes (<ns0:submitCdus1Response MessageId="xxx">) but XML elements (<MessageId>xxx</MessageId>).
Therefore all you have to is switch from #XmlAttribute to #XmlElement to match the mapping object to your actual XML.
As a note : if this comes really out of a SOAP call, chances are that you have a XSD Schema somewhere that defines the XML structure that is used here. JAXB comes with a code generation tool that will create the JAXB Objects (e.g. TravelHistoryResponseDTO) for you, deriving the data from the XSD. The tool is called xjc (you can google / search for it), it comes with the JDK and or can be found in maven plugins, (and more). This is the kind of mistake the tool would have prevented.

How can I extract content between two XML tags as text using Jackson?

I have an XML with the following template:
<tag1 attr1="value1" attr2="value2">
<tag2> text </tag2>
<tag3> another text </tag3>
</tag1>
I want to extract this xml into a POJO that has 2 text fields as String and 2 fields for attributes, but I don't quite understand how to use JacksonXmlText.
First, start from some tutorials. For example, please, take a look on this page: Convert XML to JSON Using Jackson.
Some key points:
Use com.fasterxml.jackson.dataformat.xml.XmlMapper to serialize / deserialize XML
Use annotation from this package com.fasterxml.jackson.dataformat.xml.annotation
Your code could look like below:
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
public class Main {
public static void main(String[] args) throws Exception {
XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Tag1 tag1 = new Tag1();
tag1.setTag2("text");
tag1.setTag3("another text");
tag1.setAttribute1("value1");
tag1.setAttribute2("value2");
String xml = mapper.writeValueAsString(tag1);
System.out.println(xml);
System.out.println(mapper.readValue(xml, Tag1.class));
}
}
#JacksonXmlRootElement(localName = "tag1")
class Tag1 {
private String attribute1;
private String attribute2;
private String tag2;
private String tag3;
#JacksonXmlProperty(isAttribute = true)
public String getAttribute1() {
return attribute1;
}
public void setAttribute1(String attribute1) {
this.attribute1 = attribute1;
}
#JacksonXmlProperty(isAttribute = true)
public String getAttribute2() {
return attribute2;
}
public void setAttribute2(String attribute2) {
this.attribute2 = attribute2;
}
public String getTag2() {
return tag2;
}
public void setTag2(String tag2) {
this.tag2 = tag2;
}
public String getTag3() {
return tag3;
}
public void setTag3(String tag3) {
this.tag3 = tag3;
}
#Override
public String toString() {
return "Tag1{" +
"attribute1='" + attribute1 + '\'' +
", attribute2='" + attribute2 + '\'' +
", tag2='" + tag2 + '\'' +
", tag3='" + tag3 + '\'' +
'}';
}
}
Above code prints:
<tag1 attribute1="value1" attribute2="value2">
<tag2>text</tag2>
<tag3>another text</tag3>
</tag1>
Tag1{attribute1='value1', attribute2='value2', tag2='text', tag3='another text'}

Eclipse MOXy Unmarshall Exception: Missing class indicator field from database row

I am having issue while unmarshalling an XML message. However, marshaling works fine. I am using inheritance via XML class extractor annotation.
Can you please help me identify the issue here?
Exception:
Caused by: Exception [EclipseLink-44] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class indicator field from database row [org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl#631330c].
Descriptor: XMLDescriptor(main.TransitionGuard --> [])
at org.eclipse.persistence.exceptions.DescriptorException.missingClassIndicatorField(DescriptorException.java:961)
at org.eclipse.persistence.internal.oxm.XMLRelationshipMappingNodeValue.processChild(XMLRelationshipMappingNodeValue.java:85)
at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.startElement(XMLCompositeObjectMappingNodeValue.java:385)
at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.startElement(UnmarshalRecordImpl.java:864)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:138)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:102)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:89)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:940)
at org.eclipse.persistence.internal.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:655)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:637)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:216)
... 1 more
Here are my classes:
Transition
public class Transition {
private String from;
private String to;
TransitionGuard guard;
public Transition() {
// For Moxy
}
public Transition(String from, String to) {
this(from, to, null);
}
public Transition(String from, String to, TransitionGuard guard) {
setFrom(from);
setTo(to);
setTransitionGuard(guard);
}
#XmlAttribute(name = "from")
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
#XmlAttribute(name = "to")
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
#XmlElement(name = "guard")
public TransitionGuard getTransitionGuard() {
return guard;
}
public void setTransitionGuard(TransitionGuard guard) {
this.guard = guard;
}
}
TransitionGuard
#XmlClassExtractor(TransitionGuardClassExtractor.class)
#XmlSeeAlso({ ExceptionGuard.class, ScriptedGuard.class })
public abstract class TransitionGuard {
}
TransitionGuardClassExtractor
public class TransitionGuardClassExtractor extends ClassExtractor {
#Override
public Class extractClassFromRow(Record record, Session session) {
if (null != record.get("/abpm:exception-guard")) {
return ExceptionGuard.class;
} else if (null != record.get("/abpm:scripted-guard")) {
return ScriptedGuard.class;
}
return null;
}
}
ScriptedGuard
public class ScriptedGuard extends TransitionGuard {
String script;
public ScriptedGuard() {
}
public ScriptedGuard(String script) {
setScript(script);
}
#XmlPath("abpm:scripted-guard/text()")
#XmlCDATA
public String getScript() {
return script;
}
public void setScript(String script) {
this.script = script;
}
}
TestPE
#XmlRootElement(name = "TestPE")
public class TestPE {
List<Transition> transition;
public List<Transition> getTransition() {
return transition;
}
public void setTransition(List<Transition> transition) {
this.transition = transition;
}
}
package-info
#XmlSchema(namespace = "jelly:com.werken.blissed.jelly.BlissedTagLibrary", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = {
#XmlNs(prefix = "version", namespaceURI = "4.7"), #XmlNs(prefix = "j", namespaceURI = "jelly:core"),
#XmlNs(prefix = "abpm", namespaceURI = "jelly:com.adeptia.indigo.jelly.IndigoTagLibrary"),
#XmlNs(prefix = "pd", namespaceURI = "jelly:com.adeptia.indigo.pd.ProcessDesignerTagLibrary"),
#XmlNs(prefix = "", namespaceURI = "jelly:com.werken.blissed.jelly.BlissedTagLibrary") })
package main;
import javax.xml.bind.annotation.XmlNs;
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<TestPE xmlns:version = "4.7" xmlns:j = "jelly:core" xmlns:abpm = "jelly:com.adeptia.indigo.jelly.IndigoTagLibrary" xmlns:pd = "jelly:com.adeptia.indigo.pd.ProcessDesignerTagLibrary" xmlns = "jelly:com.werken.blissed.jelly.BlissedTagLibrary" >
<transition from="state-BPMN:GATEWAY:XOR_DATA_GATEWAY-892276265" to="state-BPMN:EVENT:END_EVENT-892276264" >
<guard>
<abpm:scripted-guard><![CDATA[*** Script ***]]></abpm:scripted-guard>
</guard>
</transition>
</TestPE>
Demo code
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.xml.bind.context.factory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
JAXBContext jc = JAXBContext.newInstance(new Class[] { TestPE.class }, properties);
// JAXB unmarshall
Unmarshaller unmarshaller = jc.createUnmarshaller();
TestPE testPE = (TestPE) unmarshaller.unmarshal(new File("resources/Transition.xml"));
Currently, MOXy requires that the inheritance indicator is in an XML attribute. If it is in an XML element you could use the following approach with an XmlAdapter.

java soap web service how to get a list from a xml request

I work with a SOAP web service. I want to recover the list productOrder from a java. My problem is that the list productOrder recovered from the java class is zero.
I dont't know how to recover this list from the java.
Below I explain my classes:
This is the request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://uciext.ws.hw3/wsdl">
<soapenv:Header/>
<soapenv:Body>
<wsdl:processOrder>
<!--Optional:-->
<arg0>
<vendorCode>330029</vendorCode>
<vendorName>My Shop</vendorName>
<orderNumber>1000339</orderNumber>
<!--1 or more repetitions:-->
<wsdl:productOrder>
<!--Optional:-->
<productSku>111</productSku>
<!--Optional:-->
<productName>Kindle Fire</productName>
<!--Optional:-->
<orderQuantity>5</orderQuantity>
</wsdl:productOrder>
</arg0>
</wsdl:processOrder>
</soapenv:Body>
</soapenv:Envelope>
I have the Order.java to parse this xml
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"vendorCode",
"vendorName",
"orderNumber",
"productOrder"
})
#XmlRootElement(name = "order")
public class Order {
#XmlElement(required = true)
protected String vendorCode;
#XmlElement(required = true)
protected String vendorName;
#XmlElement(required = true)
protected String orderNumber;
#XmlElement(required = true)
protected List<ProductOrder> productOrder;
public String getVendorCode() {
return vendorCode;
}
public void setVendorCode(String value) {
this.vendorCode = value;
}
public String getVendorName() {
return vendorName;
}
public void setVendorName(String value) {
this.vendorName = value;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String value) {
this.orderNumber = value;
}
public List<ProductOrder> getProductOrder() {
if (productOrder == null) {
productOrder = new ArrayList<ProductOrder>();
}
return this.productOrder;
}
}
I have a method processOrder in a interface and their implementation. This
is the interface
#WebService(targetNamespace = "http://uciext.ws.hw3/wsdl")
public interface OrderServiceWS {
#WebMethod
OrderConfirm processOrder(#WebParam(name="arg0", mode=Mode.IN) Order order) throws Exception;
}
processOrder is in the class
#XmlRootElement(name = "processOrder", namespace = "http://uciext.ws.hw3/wsdl")
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "processOrder", namespace = "http://uciext.ws.hw3/wsdl")
public class ProcessOrder {
#XmlElement(name = "arg0", namespace = "")
private com.uciext.ws.hw3.service.model.order.Order arg0;
public com.uciext.ws.hw3.service.model.order.Order getArg0() {
return this.arg0;
}
public void setArg0(com.uciext.ws.hw3.service.model.order.Order arg0) {
this.arg0 = arg0;
}
}
This is the interface implementation where is called the method processOrder.
#WebService(
portName = "OrderPort",
serviceName = "OrderService",
targetNamespace = "http://uciext.ws.hw3/wsdl",
endpointInterface = "com.uciext.ws.hw3.service.OrderServiceWS")
public class OrderServiceWSImpl implements OrderServiceWS {
private InventoryManagerImpl manager = new InventoryManagerImpl();
#Override
public OrderConfirm processOrder(Order order) throws Exception {
Util.log("SOAP processOrder request [ order=" + order);
Util.log("OrderServiceWS start processOrder");
OrderDAO orderDAO = new OrderDAO();
List<ProductOrder> productOrderList = new ArrayList<ProductOrder>();
List<ProductOrderDAO> productOrderDAOList = new ArrayList<ProductOrderDAO>();
ProductOrderDAO productOrderDAO1 = null;
ProductDAO productDAO = null;
OrderConfirm orderConfirm = null;
ProductOrder productOrder = null;
ProductConfirm productConfirm = null;
List<ProductConfirm> productConfirmList = new ArrayList<ProductConfirm>();
Double totalPrice = 0.0;
Util.log("OrderServiceWS start processOrder vendorCode "+order.getVendorCode());
orderDAO.setVendorCode(order.getVendorCode());
orderDAO.setVendorName(order.getVendorName());
orderDAO.setOrderId(Long.parseLong(order.getOrderNumber()));
Util.log("OrderServiceWS processOrder before productOrderList ");
productOrderList = order.getProductOrder();
Util.log("OrderServiceWS processOrder before productOrderList size "+productOrderList.size());
The value of vendorCode is right but productOrderList.size() is cero

How to unmarshall this XML file structure

How would I go about unmarshalling this XML file structure with JAXB:
<document>
<properties>
<basic>
<property id="generationDate">
<value>20150525</value>
</property>
<property id="hostAddress">
<value>192.168.0.250</value>
</property>
</basic>
</properties>
</document>
Snippet from Java class
import javax.xml.bind.annotation.*;
#XmlRootElement(name = "document")
#XmlAccessorType(XmlAccessType.FIELD)
class PDFDocument {
#XmlID
#XmlAttribute(name = "generationDate")
private String generationDate;
Snippet from Unmarshalling code:
PDFDocument doc = new PDFDocument();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(PDFDocument.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
doc = (PDFDocument) jaxbUnmarshaller.unmarshal(new File(filePath));
} catch (JAXBException ex) {
Logger.getLogger(FileFunctions.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(doc.getGenerationDate());
But i'm not sure how to reference each value of a property.
Ok so basically your Java Object Structure is wrong. You need to analyze the XML and then you need to build the Java Object structure accordingly.
If you look at the XML, you have a Document, and inside the Document you have Properties, inside properties the basic properties and so on.
So similarly you need to have java classes, A Document Class, inside document you need to have properties class, inside that Basic class and so on. I have shown the class structure below, so that you can get an idea.
Document Class -
#XmlRootElement(name = "document")
#XmlAccessorType(XmlAccessType.FIELD)
public class PDFDocument {
#XmlElement(name = "properties")
private DocumentProperty documentProperty;
public DocumentProperty getDocumentProperty() {
return documentProperty;
}
public void setDocumentProperty(DocumentProperty documentProperty) {
this.documentProperty = documentProperty;
}
#Override
public String toString() {
return "PDFDocument{" +
"documentProperty=" + documentProperty + "\n" +
'}';
}
}
Class to Hold Properties -
#XmlRootElement(name = "properties")
#XmlAccessorType(XmlAccessType.FIELD)
public class DocumentProperty {
#XmlElement(name = "basic")
private Basic basic;
public Basic getBasic() {
return basic;
}
public void setBasic(Basic basic) {
this.basic = basic;
}
#Override
public String toString() {
return "DocumentProperty{" +
"basic=" + basic + "\n" +
'}';
}
}
Basic Class -
#XmlRootElement(name = "basic")
#XmlAccessorType(XmlAccessType.FIELD)
public class Basic {
#XmlElementRef
private List<Property> propertyList;
public List<Property> getPropertyList() {
return propertyList;
}
public void setPropertyList(List<Property> propertyList) {
this.propertyList = propertyList;
}
#Override
public String toString() {
return "Basic{" +
"propertyList=" + propertyList + "\n" +
'}';
}
}
Property Class -
#XmlRootElement(name = "property")
#XmlAccessorType(XmlAccessType.FIELD)
public class Property {
#XmlAttribute(name = "id")
private String id;
#XmlElement(name = "value")
private String value;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
#Override
public String toString() {
return "Property{" +
"id='" + id + "\n" +
", value='" + value + "\n" +
'}';
}
}
The Above Class structure works with the XML you provided. But I guess you will have to change this structure since There will be other types of Properties in Document other than Basic Type. In that Case I suggest you to have abstract Document Property class and to extend that to Basic and Other types of Properties.
The Test class -
public class XmlTest {
#Test
public void testXml() throws Exception {
String xml = "<document>" +
" <properties>" +
" <basic>" +
" <property id=\"generationDate\">" +
" <value>20150525</value>" +
" </property>\n" +
" <property id=\"hostAddress\">" +
" <value>192.168.0.250</value>" +
" </property>" +
" </basic>" +
" </properties>" +
"</document>";
try {
JAXBContext jaxbContext = JAXBContext.newInstance(PDFDocument.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
PDFDocument document = (PDFDocument) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes()));
System.out.println("PDF Document Structure -" +document.toString());
for(Property property : document.getDocumentProperty().getBasic().getPropertyList()) {
if(property.getId().equals("generationDate")){
System.out.println("Generation Date : "+property.getValue());
}
}
} catch (JAXBException ex) {
ex.printStackTrace();
}
}
}
Running the Test class Produces the Following Result.
PDFDocument{
documentProperty=
DocumentProperty{
basic=Basic{
propertyList=[
Property{
id='generationDate
, value='20150525
},
Property{id='hostAddress
, value='192.168.0.250
}]
}
}
}
Hope this Helps.

Categories

Resources