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>
Related
I am having below xml structure. And need to unmarshall using JAXB.
`
<Rule>
<RuleNumber>5001</RuleNumber>
**<RuleA>**
<Match> some text-1 </Match>
<Ignore> some text-2 </Ignore>
<MatchWord> some text-3 </MatchWord>
**</RuleA>**
**<RuleB>**
<Ignore> some text-4 </Ignore>
<MatchWord> some text-5 </MatchWord>
**</RuleB>**
</Rule>`
In the above xml , in level 1 <Rule> tag is there. It is known before unmarshalling.
In level -2 , <RuleNumber> , <RuleA> , <RuleB> tags are there. In this level-2 the Tag <RuleNumber> is known before unmarshalling but the other two tag's names are not known. i.e. <RuleA>, <RuleB> tags are not known before unmarshalling. And one more thing instead of <RuleA> or <RuleB> , the name of these tags may be any name. It may be <RuleC> or <RuleZ> or even <Rule101>, <Rule102>, <RuleA1> , <RuleZ1> etc.
In level-3 , <Match> , <Ignore>, <MatchWord> tags are there. Theses are known tags before unmarshalling.
I want to unmarshall this xml using jaxb . Here the name of the tag <RuleA> , <RuleB> are not known , the name can be anything. So how to write a class to map this kind of xml structure?. And subsequently needed to unmarshalling the data.
Is it possible to handle this scenario using JAXB ?
I know to write code for simple unknown type using #xmlAnyElement, i.e. if unknown tags are not having any child elements. But don't know how to approach this complex type. In my case the unknown tags having child tags.
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
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 have an XML structure of a message received from an XMPP subscription (below). I only care about the "user" part of this message and would like to convert this into an equivalent "User" object in Java so that I can use it to perform other processing. Is there a way to achieve this in Java?
The only way I know I can do it is to use Jackson annotations (e.g #JsonProperty) and create the equivalent objects for all the parent elements - event, notification, update, data etc - but I don't really care about them so seems like a waste.
Not sure how I can just convert the "user" part to an object and forget about the rest?
<event xmlns='http://jabber.org/protocol/pubsub#event'>
<notification xmlns='http://jabber.org/protocol/pubsub'>
<Update>
<data>
<user>
<dialogs>/finesse/api/User/1234/Dialogs</dialogs>
<extension></extension>
<firstName>1234</firstName>
<lastName>1234</lastName>
<loginId>1234</loginId>
<loginName>1234</loginName>
<roles>
<role>Agent</role>
</roles>
<state>LOGOUT</state>
<stateChangeTime>2015-03-11T14:25:42Z</stateChangeTime>
<teamId>1</teamId>
<teamName>Default</teamName>
<uri>/finesse/api/User/1234</uri>
</user>
</data>
</Update>
</notification>
</event>
It's a little bit ugly and not optimal for huge xml data, but you could extract user part from xml using for example dom4j and than use Jackson to parse "only user xml" part.
Document doc = new SAXReader().read(...);
Node user = doc.getRootElement()
.element("notification")
.element("Update")
.element("data")
.element("user");
String onlyUserXml = user.asXML();
I am getting response XML, in that I want to add xmlns attribute in each child node which is generated.
Present output:
<createProfileResponse xmlns="http://services.profile.webservices.ecaas.com">
<createProfileReturn>STRING</createProfileReturn>
</createProfileResponse>
Required output:
<createProfileResponse xmlns="http://services.profile.webservices.ecaas.com">
<createProfileReturn xmlns="">STRING</createProfileReturn>
</createProfileResponse>
How do I do this?
NOTE: I've used JAXB to generate the XML.
The problem is that you need to have "createProfileReturn" in the blank namespace, and you explicitly put the default namespace in a non-blank namespace in the surrounding tag.
If the XML parser is fully compliant you could create a "ecaaas" global namespace and use
<ecaas:createProfileResponse>
<createProfileReturn/>
</ecaas:createProfileResponse>
HIThanks for helping out, actually this we done in coding through the saopBinding class.
But we also modified the server-config.wsdd file I really didnt understand why we need wsdd file..
This gives only the service?.
Anil