I want to know how I can generate the following using JAXB.
Main problem - having two namespaces in soapenv. When I make the xml and then add the soap env, it adds name spaces in different lines. This is not the way I want it
Problem 2 - instead of xmlsn="..." I want xmlns:SOME_TEXT="..." as shown below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fis="http://fis.certegy.cka.com/">
<soapenv:Body>
<fis:InquiryRequest>
<Version>1.0</Version>
<RequestUID>191919191919191</RequestUID>
<Station>1078686704</Station>
<TranType>40</TranType>
<Consumer>
<FirstName>Susie</FirstName>
<LastName>Smith</LastName>
<Address>
<Line1>100 59th</Line1>
<Line2>Ave NE</Line2>
<City>New York</City>
<State>NY</State>
<Zip>10021</Zip>
</Address>
<Phone>1114589658</Phone>
<EmailAddress>Your.Email#yahoo.com</EmailAddress>
<DateOfBirth>1960-01-01</DateOfBirth>
<ID>
<Type>NY</Type>
<Value>285756967</Value>
</ID>
<DeviceID>12345678000</DeviceID>
<DaysOfEmployment>9991</DaysOfEmployment>
<PayDate>2019-05-03</PayDate>
<PayFrequency>Weekly</PayFrequency>
</Consumer>
<Amount>70.00</Amount>
<CashBack>0</CashBack>
<GiftCard>0</GiftCard>
<Check>
<Micr>
<ExpansionType>1002</ExpansionType>
<Line>T861000016A100002106C</Line>
<Swiped>false</Swiped>
</Micr>
<Type>P</Type>
</Check>
</fis:InquiryRequest>
</soapenv:Body>
</soapenv:Envelope>
Please note: I am using the EclipseLink MOXY which is an extension of the JAXB and provides a lot of additional benefits compared to normal JAXB. The below answer is based on the MOXY not sure if it would work even for Standard JAXB.
I am not sure if I have understood your problem 1 correctly. Based on my understanding when you solve the problem-2 then I am guessing it should solve this as well. As it will add all the namespaces to the header of the XML in your case soapenv:Envelope.
With regards to your problem 2:
If you would like to obtain the custom prefix for your namespace URI then you can create a Map<String, String> with your prefix and namespace and pass it during the marshaling so the moxy would automatically handle and provide the prefix to header (xmlns) and your XML node.
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put("http://schemas.xmlsoap.org/soap/envelope/", "soapenv");
namespaces.put("http://fis.certegy.cka.com/", "fis");
namespaces.put("Whatever your namespace", "SOME_TEXT");
jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
jsonMarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespaces);
Reference documentation
GitHub code
Related
Recently when i worked with JAXB i had to remove xml header from generated file. I mean, i had to remove first line in xml file which looks like in this way:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
After few minutes i found on internet, that i can use one of this solution:
marshaller.setProperty("jaxb.fragment", true);
or
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false);
Both worked for me... but what is difference between them?
NOTE: I'm using Java 12
I created a MCVE which can be found here https://github.com/starwarsjk/jaxb-remove-xml-header
"com.sun.xml.bind.xmlDeclaration" is from JAXB 1 but still supported in JAXB 2.
Marshaller.JAXB_FRAGMENT is equivalent but preferred going forward.
I am not a expert on Java but looking for help on issue we are seeing. With our java Jackson response , we are trying to build a XML based response but response is coming without a namespace prefix like below
Kindly help
<EmployeeResponse **xmlns**="http://test.service.net/com/v1">
<Reference>SequenceTime="2019-03-07 12.15.01.970236"</Reference>
<Details>
<Name>
<FirstName>Alex</FirstName>
</Name>
</Details>
</EmployeeResponse>
However we would like to have the response defined as a space prefix like below where v1 would denote a namespace type and same should be mapped to all child elemetns.
<v1:EmployeeResponse xmlns:v1="http://test.service.net/com/v1">
<v1:Reference>SequenceTime="2019-03-07 12.15.01.970236"</v1:Reference>
<v1:Details>
<v1:Name>
<v1:FirstName>Alex</v1:FirstName>
</v1:Name>
</v1:Details>
</v1:EmployeeResponse>
I am trying to create xml file using java. where the expected output is given below
EXPECTED OUTPUT
<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0</cbc:CustomizationID>
<cbc:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
<cbc:ID>2019-112</cbc:ID>
<cbc:IssueDate>2019-01-21</cbc:IssueDate>
<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
</Invoice>
ACTUAL OUTPUT
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:ns3="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ns4="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0</CustomizationID>
<ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</ProfileID>
<InvoiceTypeCode>380</InvoiceTypeCode>
</ns4:Invoice>
For clear understanding, lets take "cbc:CustomizationID" in expected output, but in actual output only "CustomizationID"
The actual output is the same as the expected. Just changes how the namespaces are applied.
The XML Parser should treat it exactly the same.
Take a look at XML namespaces.
I have changed the #XmlElement(name = "cbc:CustomizationID", required = true) after generating java class using xjc command. partially issue is solved but needs to change root element to inserted of
We are using ColdFusion and Java to generate the Twilio Markup / XML necessary for handling Twilio calls in our webhook.
Currently, everything works well. The output xml/twiml generated looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial callerId="+18184461999">
<Number>+18554904999</Number>
</Dial>
</Response>
We generated this markup using java classes in ColdFusion, mainly because ColdFusion can't do it natively. This is the ColdFusion/Java code we currently use to generate the above xml:
<cfscript>
TWILIO_CALLER_ID = "+18184461999";
tophn="+18554904999";
objPattern = CreateObject("java","java.util.regex.Pattern").Compile(JavaCast( "string", "^[\\d\\+\\-\\(\\) ]+$"));
objMatcher=objPattern.Matcher(JavaCast( "string", tophn ));
dialBuilder = createObject("java","com.twilio.twiml.Dial$Builder").init();
dialBuilder.callerId(TWILIO_CALLER_ID);
numberbuilder= createObject("java","com.twilio.twiml.Number$Builder").init(tophn).build();
dialBuilder = dialBuilder.number(numberbuilder);
voiceTwimlResponse = createObject("java","com.twilio.twiml.VoiceResponse$Builder").dial(dialBuilder.build()).build();
response = '<?xml version="1.0" encoding="UTF-8"?>' & voiceTwimlResponse.toXml();
</cfscript>
Everything above works perfectly for our needs.
However, now we would like to add an attribute to the "Dial" element: record="RECORD_FROM_RINGING". Below is what the XML we would like generated would look like:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial callerId="+18184461999" record="RECORD_FROM_RINGING">
<Number>+18554904999</Number>
</Dial>
</Response>
How would we use ColdFusion + java to accomplish this? We have spent hours trying to figure this out, and nothing works for us. We have looked into the Record and Record$Builder classes, but found nothing that adds this attribute the way we need it. The closest we got to was being able to add a <Record /> element before the <Dial> element, but that does not work for us.
How do we add the attribute record="RECORD_FROM_RINGING" to the <Dial> element using ColdFusion and the appropriate Java classes/objects? All we require is the attribute to be set for that element.
My guess is you are using an older version of the jar which does not support all of the <Dial> attributes. Looks like the Twilio instructions link to an older version (currently 7.0.0). The GitHub version is already up to 7.8.0. Try downloading 7.8.0 or building a newer version from the source (do not forget the dependencies).
The Dial.Builder class in 7.8.0 contains a new method named options(String key, String value) which supports arbitrary attributes. Use it to set the "record" attribute like this:
...
recordOption = createObject("java","com.twilio.twiml.Dial$Record");
dialBuilder.options("record", recordOption.RECORD_FROM_RINGING.toString());
...
** Using the Dial.Record enum, instead of a hard coded string, helps insulate the code against changes in the API.
Result:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial callerId="+18185551999" record="record-from-ringing">
<Number>+18185554999</Number>
</Dial>
</Response>
I want to parse the following xml snippet. Can anyone help me out in this. I am not getting how to parse through the elements and all and just retrieve the and EmployeeId nodes and its values
<soapenv:Envelope xmlns:soapenv="http://schemas.xxx/yyy" xmlns:idi="http://xxxx/">
<soapenv:Header/>
<soapenv:Body>
<idi:retrieve>
<requestXML>
<CustomerNumber>111</CustomerNumber>
<EmployeeId>222</EmployeeId>
</requestXML>
</idi:retrieve>
</soapenv:Body>
</soapenv:Envelope>
Please see the xml above and give some hinte to parse this.
Thank you
You should use a framework that knows to create an object model from a WSDL and do the parsing for you - that is the best course of action.
Just google on any of these:
Axis2
JAX-WS
JAX-RPC
Choose one and use it.