This discussion is archived
5 Replies Latest reply: Nov 17, 2011 9:03 AM by DrClap RSS

string to xml object java

897874 Newbie
Currently Being Moderated
Hello,

can anyone suggest me how to convert string to xml object in java?
I have a string (Data) ant now I have to pass it as parameter as xml object, which structure should be:
<MGWData>Data</MGWData>
Where I surf I always see only solutions for converting string to xml document, but never to xml object.
maybe the problem is I don't completely understand what xml object is in first place :/

Any answer would help.
Thanks.
  • 1. Re: string to xml object java
    thedatawarehouse Newbie
    Currently Being Moderated
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Text;
    
    ...
    
    final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = null;
            try {
                docBuilder = dbfac.newDocumentBuilder();
            } catch (final ParserConfigurationException e) {
                e.printStackTrace();
            }
            final Document doc = docBuilder.newDocument();
            final Element rootElement = doc.createElement("MGWData");
            doc.appendChild(rootElement);
    
            final Text textValue = doc.createTextNode("Data");
            rootElement.appendChild(textValue);
  • 2. Re: string to xml object java
    maheshguruswamy Journeyer
    Currently Being Moderated
    thedatawarehouse wrote:
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Text;
    
    ...
    
    final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    try {
    docBuilder = dbfac.newDocumentBuilder();
    } catch (final ParserConfigurationException e) {
    e.printStackTrace();
    }
    final Document doc = docBuilder.newDocument();
    final Element rootElement = doc.createElement("MGWData");
    doc.appendChild(rootElement);
    
    final Text textValue = doc.createTextNode("Data");
    rootElement.appendChild(textValue);
    I highly doubt this non-working piece of code will help the OP. @OP, an XML object is just like any other java object. Its a class with fields that describe the XML tree. One commonly used way to represent a XML tree in Java is using the Document interface. Check out http://download.oracle.com/javase/6/docs/api/org/w3c/dom/Document.html. Then google for how to convert a XML string to a Document.
  • 3. Re: string to xml object java
    897874 Newbie
    Currently Being Moderated
    here is the method to call:

    +/**+
    * Sets the p_DATAXMLTYPEIN value for this SVARCHAR2INSERT_RECEIVESInput.
    *+
    * @param p_DATAXMLTYPEIN
    */
    *public void setP_DATAXMLTYPEIN(com.oracle.xmlns.orawsv.SISTEMA_MOKA.MGW_PUBLIC_FUNCTIONS.SVARCHAR2INSERT_RECEIVESInputP_DATAXMLTYPEIN p_DATAXMLTYPEIN) {*
    this.p_DATAXMLTYPEIN = p_DATAXMLTYPEIN;
    *}*

    one of the classes contains:

    *public class SVARCHAR2INSERT_RECEIVESInputP_DATAXMLTYPEIN  implements java.io.Serializable, org.apache.axis.encoding.AnyContentType {*
    *private org.apache.axis.message.MessageElement [] _any;*
    ...
    this method and some classes where generated using eclipse and wsdl file by creating web service client.
    wsdl says for that part: *<xsd:element name="P_DATA-XMLTYPE-IN">*
    So I suppose it should involve smth org.apache.axis.message.MessageElement. But when I create any type of parameter, it gives me error:
    + ....cannot be cast to com.oracle.xmlns.orawsv.SISTEMA_MOKA.MGW_PUBLIC_FUNCTIONS.SVARCHAR2INSERT_RECEIVESInputP_DATAXMLTYPEIN+
    I am new in web service stuff so thank you for response.
  • 4. Re: string to xml object java
    maheshguruswamy Journeyer
    Currently Being Moderated
    894871 wrote:
    here is the method to call:

    +/**+
    * Sets the p_DATAXMLTYPEIN value for this SVARCHAR2INSERT_RECEIVESInput.
    *+
    * @param p_DATAXMLTYPEIN
    */
    *public void setP_DATAXMLTYPEIN(com.oracle.xmlns.orawsv.SISTEMA_MOKA.MGW_PUBLIC_FUNCTIONS.SVARCHAR2INSERT_RECEIVESInputP_DATAXMLTYPEIN p_DATAXMLTYPEIN) {*
    this.p_DATAXMLTYPEIN = p_DATAXMLTYPEIN;
    *}*

    one of the classes contains:

    *public class SVARCHAR2INSERT_RECEIVESInputP_DATAXMLTYPEIN  implements java.io.Serializable, org.apache.axis.encoding.AnyContentType {*
    *private org.apache.axis.message.MessageElement [] _any;*
    ...
    this method and some classes where generated using eclipse and wsdl file by creating web service client.
    wsdl says for that part: *<xsd:element name="P_DATA-XMLTYPE-IN">*
    So I suppose it should involve smth org.apache.axis.message.MessageElement. But when I create any type of parameter, it gives me error:
    + ....cannot be cast to com.oracle.xmlns.orawsv.SISTEMA_MOKA.MGW_PUBLIC_FUNCTIONS.SVARCHAR2INSERT_RECEIVESInputP_DATAXMLTYPEIN+
    I am new in web service stuff so thank you for response.
    Can you explain in plain English what you are trying to do? Or provide a [url http://sscce.org/]SSCE.
  • 5. Re: string to xml object java
    DrClap Expert
    Currently Being Moderated
    894871 wrote:
    Where I surf I always see only solutions for converting string to xml document, but never to xml object.
    maybe the problem is I don't completely understand what xml object is in first place :/
    Yeah. That's my question too. What's a "XML object"? But if you don't know what it is, but you think you need one, then you should definitely go and find out what exactly it is that you need before asking the question.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points