Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I've been doing a lot of looking on both stackoverflow and google in general to try and answer my following question, but I have been unable to find anything that can help me to get this work 100%. I'm pretty sure I have everything except a SMALL error correct, but obviously you guys probably have suggestions anyway, so go for it!

And, here we go: I've been using HTTPClient to test an API on a few different environments, and I got HTTPPost methods to accept JSON payloads but now I'm trying to send payloads using XML and I'm running into some issues. It seems that the XML string that I'm creating (in the code below) is correct... so I'm stumped as to why this isn't working. ALSO: got most of the DOM code from the internet (to build up the XML payload) so feel free to bring that into question as well...

My code is the following:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();
Element subscription = doc.createElement("subscription");
doc.appendChild(subscription);

subscription.setAttribute("email", "[email protected]");
etc....
etc....
etc....
etc....

DOMSource domSource = new DomSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);

String XMLpayload = writer.toString();

[name of my HttpRequest].setEntity(new StringEntity(XMLpayload));
[name of my HttpResponse] = client.execute(request);

now... I'm looking to achieve the payload seen BELOW:

<subscription>
    <email>[email protected]</email>
    <firstName>Patricia</firstName>
    <lastName>Miller</lastName>
    <title>Ms.</title>
    <gender>Female</gender>
</subscription>

When I print out the payload that I am sending currently it looks like the following:

?xml version="1.0" encoding="UTF-8" standalone="no"? subscription email="[email protected]" firstName="Patricia" gender="Female" lastName="Miller" title="Ms."/

(NOTE: I REMOVED THE < AND > BRACKETS. THEY APPEAR WHERE THEY SHOULD!)

But, I'm receiving a 400 error. Any ideas here? I know I have the proper headers, the URL is correct, etc. It's absolutely something with what I'm doing with the payload. Any thoughts would be greatly appreciated!!

Best!

share|improve this question
2  
Is the generated xml payload from your code correct? Per code, the email, firstname, etc.. are set as attributes of your 'subscription' element. If you need 'email','firstname',etc.. as child elements, you should use appendChild() instead of setAttribute() –  user1573133 Jul 1 at 21:01
 
Please go back and put in the < and > and indent the whole thing 4 spaces, so we don't have to guess or read your mind. If it's all one line, leave it on one line and let SO display it in a scrolling code widget. –  Jim Garrison Jul 1 at 21:13
 
@user1573133 Your comment (without the first sentence) should be an answer. –  Jim Garrison Jul 1 at 21:15
 
@JimGarrison: Thx. Added as an answer. –  user1573133 Jul 2 at 2:08

1 Answer

up vote 2 down vote accepted

In your expected payload, 'email','firstname',etc.. are child elements of Subscription element. As per the code, they are added as attributes of your 'subscription' element. If you need 'email','firstname',etc.. as child elements, you should use appendChild() instead of setAttribute().

Element email = doc.createElement("email");
email.appendChild(document.createTextNode("[email protected]"));
subscription.appendChild(email);
share|improve this answer
 
oh wow, thanks a lot! that's the kind of thing that I was confused about. I'm extremely new to XML and JSON in java so getting the correct payload is tough but this is extremely helpful! Gonna go try it out! I feel like that was the issue though! THANKS!!! –  user2529603 Jul 2 at 12:49
 
IT WORKED! you're a life saver!!! it's the simple things like that that I love about programming. so subtle, yet so important. thanks again ! –  user2529603 Jul 2 at 13:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.