Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6739795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:31:49+00:00 2026-05-26T11:31:49+00:00

I have a java web service that returns a string. I’m creating the body

  • 0

I have a java web service that returns a string. I’m creating the body of this xml string with a DocumentBuilder and Document class. When I view source of the returned XML (Which looks fine in the browser window) instead of <> it’s returning &lt; and &gt; around the XML nodes.

Please help.

****UPDATE (including code example)
The code is not including any error catching, it was stripped for simplicity.
One code block and three methods are included:
The first code block (EXAMPLE SETUP) shows the basics of what the Document object is setup to be like. the method appendPayment(...) is where the actual document building happens. It calls on two helper methods getTagValue(...) and prepareElement(...)
**Note, this code is meant to copy specific parts from a pre-existing xml string, xmlString, and grap the necessary information to be returned later.

****UPDATE 2
Response added at the end of the question

************ Follow-up question to the first answer is here:
How to return arbitrary XML Document using an Eclipse/AXIS2 POJO Service

EXAMPLE SETUP
{
    //create new document
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder newDocBuilder = docFactory.newDocumentBuilder();
    Document newDoc = newDocBuilder.newDocument();
    Element rootElement = newDoc.createElement("AllTransactions");

    newDoc.appendChild(rootElement);
    appendPayment(stringXML, newDoc);
}

public static void appendPayment(String xmlString, Document newDoc) throws Exception
{
    //convert string to inputstream
    ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes());
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document oldDoc =   docBuilder.parse(bais);
    oldDoc.getDocumentElement().normalize();            

    NodeList nList = oldDoc.getChildNodes();
    Node nNode = nList.item(0);
    Element eElement = (Element) nNode;

    //Create new child node for this payment
    Element transaction = newDoc.createElement("Transaction");
    newDoc.getDocumentElement().appendChild(transaction);


    //status
    transaction.appendChild(prepareElement("status", eElement, newDoc));

    //amount
    transaction.appendChild(prepareElement("amount", eElement, newDoc));
}

private static String getTagValue(String sTag, Element eElement)
{
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0);
    return nValue.getNodeValue();
}

private static Element prepareElement(String sTag, Element eElement, Document newDoc)
{
    String str = getTagValue(sTag, eElement);
    Element newElement = newDoc.createElement(sTag);
    newElement.appendChild(newDoc.createTextNode(str));
    return newElement;
}

Finally, I use the following method to convert the final Document object to a String

public static String getStringFromDocument(Document doc)
{
    try
    {
       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);
       return writer.toString();
    }
    catch(TransformerException ex)
    {
       ex.printStackTrace();
       return null;
    }
}

The header type of the response is as follows

Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=utf-8
Transfer-Encoding: chunked

This is an example response

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <getTransactionsResponse xmlns="http://services.paypal.com">
        <getTransactionsReturn>&lt;AllTransactions&gt;&lt;Transaction&gt;&lt;status&gt;PENDING&lt;/status&gt;&lt;amount&gt;55.55&lt;/amount&gt;&lt;/transaction&gt;
        </getTransactionsResponse>
    </soapenv:Body>
</soapenv:Envelope>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:31:50+00:00Added an answer on May 26, 2026 at 11:31 am

    The framework is doing what you tell it; your method returns a String which means the generated WSDL should have a response message of type <xsd:string>. As we know, XML strings must encode certain characters as character entity references (i.e. “<” becomes “&lt;” so the XML parser treats it as a string, not the beginning of an XML element as you intend). If you want to return an XML document then you must define the XML structure in the WSDL <types> section and set the response message part to the appropriate element.

    To put it another way, you are trying to send “typed” data without using the strong type system provided by SOAP/WSDL (namely XML schema); this is generally regarded as bad design (see Loosely typed versus strongly typed web services).

    The ultimate solution is to to define the response document via a proper XML Schema. If there is no set schema, as by the design of your service, then use the <xsd:any> type for the message response type, although this approach has its pitfalls. Moreover, such a redesign implies a schema-first (top-down) development model and from the comment stream it seems that you are currently practicing code-first (bottom-up) approach. Perhaps your tools provide a mechanism such as a “general XML document” return type or annotation which achieves the same effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF web service that returns a string to a Java based
I have a JAX-RPC (Java) web service that needs to return a complex polymorphic
We have a java web service application that uses log4j to do logging. An
We have a dotnet web service that a java using customer wants to connect
I have a Java client that calls a web service at the moment using
I'm creating a web service in Java that will be consumed by an external
I have the following code in a java Web Service: public boolean makeFile(String fileName,
I have a .net web service that takes some xml data. From within the
I have a java application that has Web Services published using Axis. With the
I have a Java web service and a Java web client making use of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.