Converting XML Document to String

Dear All,
I am quite new to Java Web Services. I have made a Java Class which calls all the web services . I am displaying the results on a Command prompt. Below is my sample code:
import javax.xml.rpc.Service;
import javax.xml.rpc.Stub;
import horusWS.Horus_x0020_Web_x0020_Services_Impl;
import horusWS.Horus_x0020_Web_x0020_ServicesSoap;
public class JavaClient {
public static void main(String[] args)
Horus_x0020_Web_x0020_Services_Impl service = new Horus_x0020_Web_x0020_Services_Impl();
Horus_x0020_Web_x0020_ServicesSoap port = service.getHorus_x0020_Web_x0020_ServicesSoap();
String str;
str = port.getTextFeedbackLearner(1,"P001",1);
//where getTextFeedbackLearner is the my Web Service.
System.out.println(str);
System.out.println returns the web service in string format. Now I want to compare this string to my actual getTextFeedbackLearner.xml file. But I think I need to convert getTextFeedbackLearner.xml to string first. Please let me know
how can I convert an XML Document to String.
Thanking you in Anticipation.
cheers,
Sunil Sabir

You can read the XML document as you read any other file.
Read it into a String variable.
Check java.io.FileReader, java.io.BufferReader, etc.

Similar Messages

  • Want help in converting XML Document to a postscript file

    I want to convert XML Document to a postscript file in Java, not from command line. Any help is appreciated.

    well, you first need an XML parser, which is easy to implement
    then you need to either write the postscript out bit by bit or use a Java API that writes PostScript for you.
    It's really simple, the trick is HOW you decide to write out the PS

  • XML Document to String - Line Separator problem

    I�m facing a problem with line separator while converting a Document object to String.
    Scenario: I get an input XML having line separator say \n (hex: 0A).
    When I create the output XML using Transformer, the line separator is still \n.
    I have a requirement which makes me convert the transformed Stream into a Document.
    When I try to get the String/bytes from the Document, my line separator is now �\r\n�(hex 0D0A), which is my system�s System property �line-separator�.
    I want to keep the line separator same as that was in input string. So, if the input has the separator has \n, output should have the separator as �\n� and if the input has it as �\r\n�, output should have the same.
    i.e. my hex output should match the hex input.
    Any pointers in the direction are welcome.
    Thanks in advance.

    Can I infer that the parser has changed my document
    line separators when I load it into a Document?
    If yes, is there a way to prevent that from
    happening?I don't really know. Since the XML recommendation says that a parser "MUST" do line-break normalization, I would expect that it does. And I wouldn't design systems that use specific non-XML-approved line endings as a feature, either.

  • How to convert Xml Document into orcale tempary table

    i am creating xml document into java and passing this xml into oracle database and i need to fetch this xml into result set | rowset.
    Xml Structure
    <Department deptno="100">
    <DeptName>Sports</DeptName>
    <EmployeeList>
    <Employee empno="200"><Ename>John</Ename><Salary>33333</Salary>
    </Employee>
    <Employee empno="300"><Ename>Jack</Ename><Salary>333444</Salary>
    </Employee>
    </EmployeeList>
    </Department>
    i need like this format
    Deptno DeptName empno Ename Salary
    100 Sports 200 Jhon 2500
    100 Sports 300 Jack 3000

    It does depend on your version as odie suggests.
    Here's a way that will work in 10g...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select xmltype('<Department deptno="100">
      2  <DeptName>Sports</DeptName>
      3  <EmployeeList>
      4  <Employee empno="200"><Ename>John</Ename><Salary>33333</Salary>
      5  </Employee>
      6  <Employee empno="300"><Ename>Jack</Ename><Salary>333444</Salary>
      7  </Employee>
      8  </EmployeeList>
      9  </Department>
    10  ') as xml from dual)
    11  --
    12  -- End of test data, Use query below
    13  --
    14  select x.deptno, x.deptname
    15        ,y.empno, y.ename, y.salary
    16  from t
    17      ,xmltable('/'
    18                passing t.xml
    19                columns deptno   number       path '/Department/@deptno'
    20                       ,deptname varchar2(10) path '/Department/DeptName'
    21                       ,emps     xmltype      path '/Department/EmployeeList'
    22               ) x
    23      ,xmltable('/EmployeeList/Employee'
    24                passing x.emps
    25                columns empno    number       path '/Employee/@empno'
    26                       ,ename    varchar2(10) path '/Employee/Ename'
    27                       ,salary   number       path '/Employee/Salary'
    28*              ) y
    SQL> /
        DEPTNO DEPTNAME        EMPNO ENAME          SALARY
           100 Sports            200 John            33333
           100 Sports            300 Jack           333444
    SQL>If the XML is a string e.g. a CLOB then it can easily be converted to XMLTYPE using the XMLTYPE function.

  • Converting XML document to HTMl using xsl

    Hi,
    I'm trying to convert an xml document into html page using xsl. But when I try to open the page in the browser nothing comes up.
    I'm not sure if I am using the PrintWriter correctly.
    StreamResult result = new StreamResult(new PrintWriter(new (File"text.html")));
    Please help.

    Oops! I wrote the parenthesis wrong in the previous mail
    This is the correct one I use in my program.
    StreamResult result = new StreamResult(new PrintWriter(new File("text.html")));
    Please help. Its urgent

  • XML document - request string

    Hi Experts,
    Could you please tell me, how to pass XML document as a request string to the webservice scenario. Inside the service, I have to do the schema validation for the request . Please guide me, how to proceed further?
    Regards
    Sara

    Hi SARA,
    use following Code written in SAX parsing .this will fulfil your requirment.This handle Special character also.
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import javax.xml.parsers.FactoryConfigurationError;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.Attributes;
    import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.SAXException;
    import com.sap.aii.mapping.api.StreamTransformation;
    import com.sap.aii.mapping.api.StreamTransformationException;
    public class SimpleForwardMappingString extends DefaultHandler implements StreamTransformation
         private List m_arlCurrent = new ArrayList();
         private List m_arlBOList = new ArrayList();
         private List m_arlObjectList = new ArrayList();
        public static int counter = 0;
         String listName = "newList";
         public  StringBuffer m_totalElementsBuffer = new StringBuffer();
         String startData = "<inCanonStream>";
         String endData = "</inCanonStream>";
         private Map param = null;
         /** Constant Strings used in Forward Mapping */
         public static void main(String args[])
              try {
                      InputStream in = new FileInputStream(new File("C:
    Documents and Settings
    281152
    Desktop
    Cannonical12321.xml"));
                   OutputStream out = new FileOutputStream(new File("C:
    Documents and Settings
    281152
    Desktop
    Cannonical21.xmlOut.xml"));               
                   SimpleForwardMappingString myMapping = new SimpleForwardMappingString();
              catch (Exception e)
                   e.printStackTrace();
         public void execute(InputStream arg0, OutputStream arg1)
         throws StreamTransformationException
              Writer out;
              try
                   out = new OutputStreamWriter(arg1, "UTF16");
                   /** Building the SAX parser */
                   getXMLDoc(arg0, arg1);
                   arg1.write(m_totalElementsBuffer.toString().getBytes("UTF16"));
              catch (IOException e)
                   System.out.println("Error : io" + e.getMessage());
              catch (Exception e)
                   System.out.println("Error :" + e.getMessage());
         /* (non-Javadoc)
    @see com.sap.aii.mapping.api.StreamTransformation#setParameter(java.util.Map)
         public void setParameter(Map arg0)
         public void getXMLDoc(InputStream inputStream, OutputStream outputStream)
    Defines a factory API that enables applications to configure and
    obtain a SAX based parser to parse XML documents. Once an application
    has obtained a reference to a SAXParserFactory it can use the
    factory to configure and obtain parser instances.
              SAXParserFactory factory = SAXParserFactory.newInstance();
              try
                   factory.setNamespaceAware(true);
                   factory.setValidating(true);
                   SAXParser saxParser = factory.newSAXParser();
    Parse the content of the given {@link java.io.InputStream}
    instance as XML using the specified
    {@link org.xml.sax.helpers.DefaultHandler}.
    @param inputstream InputStream containing the content to be parsed.
    @param cobject The SAX DefaultHandler to use.
    @exception IOException If any IO errors occur.
    @exception IllegalArgumentException If the given InputStream is null.
    @exception SAXException If the underlying parser throws a
    SAXException while parsing.               
                   saxParser.parse(inputStream, this);
              catch (FactoryConfigurationError e)
                   System.out.println("Error");
              catch (ParserConfigurationException e)
                   e.printStackTrace();
              catch (SAXException e)
                   e.printStackTrace();
              catch (IOException e)
                   e.printStackTrace();
         /** ===========================================================
                             Methods Overriding in SAX Default Handler
              ===========================================================
    Receive notification of the beginning of the document.
    By overriding this method in a subclass to take specific actions
    at the beginning of a document (such as allocating the root node
    of a tree or creating an output file)     
         public void startDocument()throws SAXException
              if(m_totalElementsBuffer.length()>0)
                                m_totalElementsBuffer = new StringBuffer();
                                counter = 0;
              m_totalElementsBuffer.append(m_prologue);
              m_totalElementsBuffer.append(m_nameSpace);
              m_totalElementsBuffer.append(startData);     }
    Receive notification of the end of the document.
    By overriding this method in a subclass to take specific
    actions at the end of a document (such as finalising a tree
    or closing an output file)     
         public void endDocument()throws SAXException
              m_totalElementsBuffer.append(endData);
              m_totalElementsBuffer.append(m_rootElementEnd);
    Receive notification of the start of an element.
    By overriding this method in a subclass to take specific
    actions at the start of each element (such as allocating
    a new tree node or writing output to a file)
         public void startElement(String namespaceURI, String name, String qName, Attributes attrs)
              throws SAXException
              /** If current List is not empty then append node to StringBuffer */
                   m_totalElementsBuffer.append("&lt;" + name + "&gt;");
    Receive notification of the end of an element.
    By overriding this method in a subclass to take specific
    actions at the end of each element (such as finalising
    a tree node or writing output to a file)
         public void endElement(String uri, String name, String qName) throws SAXException
                   m_totalElementsBuffer.append("&lt;/" + name + "&gt;");
    Receive notification of character data inside an element.
    By overriding this method to take specific actions for each
    chunk of character data (such as adding the data to a node
    or buffer, or printing it to a file)     
         public void characters(char buf[], int offset, int len)
              throws SAXException
              String s = new String(buf, offset, len);
              if (null != s && s.length() >0)
                   if(s.equalsIgnoreCase("&"))
                         //System.out.println("FOUND AND SYMBOL");
                         String s1 = s.replaceAll("&","_-#_");
                         //System.out.println("s= " +s1);
                         s = s1;
                   if(s.equalsIgnoreCase("<"))
                         //System.out.println("FOUND AND SYMBOL");
                         String s1 = s.replaceAll("<","_#-_");
                         //System.out.println("s= " +s1);
                         s = s1;
                   m_totalElementsBuffer.append(s);
    Thanks
    Sunil Singh

  • How to convert XML document to xml variable?

    Hi,
    I have taken an xml variable in my process and reading a xml document file. Can anyone tell me how can i convert this xml document to xml variable so that i can parse the xml variable.
    I am able to convert xml variable to xml document variable by using set value activity. But how do I perform reverse process.
    I want to perform this conversion because i want to keep xml data in document format on storage drive and then parse this xml data so that i can read the nodes.
    Any help will be appreciated..
    Regards
    Sunil

    we can use set value activity to reach these xml docs and assign it to xml variable.
    Then parsing of this xml can be browsed to find the nodes.
    Regards
    Sunil

  • How to convert XML doc to string and vice versa?

    Assume I have a XML doc and I want to convert it into a string (and put it into a string variable).
    How can I do this?
    How can I do the opposite: Convert a string content into a XML doc?
    Peter

    Use:
    ParseEscapedXML() and ora:getContentAsString()
    See
    http://www.codeguru.com/cpp/sample_chapter/article.php/c10789__7/
    Marc

  • Converting XML Document Message (xsd string) to BPEL

    Hello, I have written a BPEL flow that takes input from a client (a query) then Invokes a scynchronous data service (passes some parameters to run a federated query) that returns an xsd string that is a set of records in XML (see below). I want to understand how I use the Assign function (or other method) to consume the xsd string (XML file) in BPEL.
    I set up complexType elements in the WSDL schema as follows (snippet, not complete WSDL) to manage the input and output:
    <element name="QueryRequest" type="tns:inputType"/>
    <element name="QueryResult" type="tns:recordType"/>
    <complexType name="inputType">
    <sequence>
    <element name="username" type="string"/>
    <element name="password" type="string"/>
    <element name="city" type="string"/>
    <element name="state" type="string"/>
    <element name="zip" type="string"/>
                             </sequence>
    </complexType>
                        <complexType name="recordType">
                             <sequence>
                                  <element name="customer" type="tns:customerType" maxOccurs="unbounded"/>
                             </sequence>
                        </complexType>
                        <complexType name="customerType">                                             
    <sequence>
    <element name="accountNumber" type="string"/>
    <element name="lastName" type="string"/>      
    <element name="firstName" type="string"/>
    <element name="email" type="string"/>
    <element name="city" type="string"/>
    <element name="state" type="string"/>
    <element name="zip" type="string"/>
                             </sequence>                         
                        </complexType>
    Information:
    - BPEL PM 2.1.2
    - BPEL Designer Eclipse Plugin 0.9.10
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <recordset>
    <customer>
    <CustNum>1</CustNum>
    <CustLName>aLName</CustLName>
    <CustFname>aFName</CustFname>
    <CustAddress>111 a street</CustAddress>
    <CustCity>austin</CustCity>
    <CustState>TX</CustState>
    <CustZip>78000</CustZip>
    </customer>
    <customer>
    <CustNum>2</CustNum>
    <CustLName>bLName</CustLName>
    <CustFname>bFName</CustFname>
    <CustAddress>112 b street</CustAddress>
    <CustCity>austin</CustCity>
    <CustState>TX</CustState>
    <CustZip>78000</CustZip>
    </customer>
    <customer>
    <CustNum>5</CustNum>
    <CustLName>eLName</CustLName>
    <CustFname>eFName</CustFname>
    <CustAddress>115 e street</CustAddress>
    <CustCity>austin</CustCity>
    <CustState>TX</CustState>
    <CustZip>78002</CustZip>
    </customer>
    <customer>
    <CustNum>6</CustNum>
    <CustLName>fLName</CustLName>
    <CustFname>fFName</CustFname>
    <CustAddress>116 f street</CustAddress>
    <CustCity>austin</CustCity>
    <CustState>TX</CustState>
    <CustZip>78002</CustZip>
    </customer>
    <customer>
    <CustNum>BOBA101</CustNum>
    <CustLName>Austin</CustLName>
    <CustFname>Bob</CustFname>
    <CustAddress>101 Main Street</CustAddress>
    <CustCity>Austin</CustCity>
    <CustState>TX</CustState>
    <CustZip>78112</CustZip>
    </customer>
    <customer>
    <CustNum>JOHNA103</CustNum>
    <CustLName>Austin</CustLName>
    <CustFname>John</CustFname>
    <CustAddress>112 Main</CustAddress>
    <CustCity>austin</CustCity>
    <CustState>tx</CustState>
    <CustZip>78113</CustZip>
    </customer>
    </recordset>
    Any and all help is greatly appreciated.

    Hi Sean,
    Can you forward your Project files to us by zipping it and renaming the .zip to .zap and send it to [email protected] with the same subject as this thread?
    Regards,
    Dhaval

  • Convert XML Node to String using XSLT

    Hi,
    Perhaps this has been discussed before. But i am looking at creating a XSLT mapping to take the entire inbound message and put it into a single node of the target.
    Is there a way?
    Thanks in advance
    Regards
    Venkat

    Hi Shankar,
    I have a input XML <root><a></a></root>
    I have a target <tgt><b></b></tgt>
    I need to have the target xml as
    <tgt><b>&lessthansign;root&greaterthansign;&lessthansign;a&greaterthansign;&lessthansign;/a&greaterthansign;&lessthansign;/root&greaterthansign;</b></tgt>
    Using the XSLT that you mentioned
    I am getting the format
    <tgt><b><root><a></a></root></b></tgt>
    Now the consumer of this message expects the data in the first type of format
    That is where i am getting stuck
    Thanks and Regards
    Venkat

  • Convert Document to String ? (Java 1.3 compatible)

    Hi folks,
    hope you can help me out. I'm trying to convert a Document to String and my code must be compatible with Java 1.3.
    Using Java 1.5 this code is working:
    public static String string_output(Document doc){
              String content = "";
              try{
                   //Console output
                   TransformerFactory tranFactory = TransformerFactory.newInstance();
                   Transformer aTransformer = tranFactory.newTransformer();     
                   Source src = new DOMSource(doc);
                   StringWriter stringWriterOutput=new StringWriter();
                   Result dest = new StreamResult(stringWriterOutput);
                   aTransformer.transform(src,dest);
                   content = stringWriterOutput.toString();
              catch (Exception e){
                   System.out.println(e.getMessage());
              return contenat;
         }     But I can't use this code in Java 1.3. What can I do (except using a higher Java version)

    I think the default transform is the only standard way of converting a Document to a String.
    Xerces has a class that does the job: org.apache.xml.serialize.DOMSerializer

  • Error converting XML to String

    I'm trying to use getXMLString() to put my XML document in a String variable. I get the following when I try to output the String:
    <?xml version = '1.0'?>
    <ERROR>oracle.xml.sql.OracleXMLSQLException: This object has been closed. If the close() was implicit, and you would like the object not to be closed between calls, see the keepCursorAlive() method</ERROR>
    If I just output the XML doc without putting it into a String variable it works fine.
    System.out.println(q.getXMLString()); //OK
    // get the XML document in string format
    String xmlString = q.getXMLString(); //NOT OK
    System.out.println(xmlString);
    What am I doing wrong? Any help would be great!
    Thanks,
    Melani

    Hi,
    SMUM_XML_PARSE is a simplified, unreleased and undocumented version of the powerful, released and documented iXML
    If you look at the GET_ELEMENT subroutine in that function module, you'll see that the nodes of type CO_NODE_CDATA_SECTION are simply ignored.
    The simpliest thing for you, maybe, is to duplicate the code of SMUM_XML_PARSE (as it is rather easy to handle), and add a section to GET_ELEMENT. You'll have also to use IF_IXML_CDATA_SECTION interface and ixml_iid_cdata_section constant.
    Documentation is here: [SAP Library: Interface if_ixml_cdata_section|http://help.sap.com/saphelp_nw70/helpdata/en/bb/576658dca511d4990b00508b6b8b11/frameset.htm ]
    Best regards,
    Sandra

  • String to XML Document

    Can anyone say how to transform a XML String to XML document
    (eg: String xmlstring="<root><main>Title</main></root>";
    How can I transform this to XML Document??
    Guhan

    Right, my mistake, this is what happens when you type code in a browser... hard to compile.
    Document XMLDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader("<root><main>Title</main></root&g t;")))PS. Consider following the online JAXP tutorial: http://java.sun.com/xml/tutorial_intro.html

  • Convert document into string with unicode

    I want to convert my document into string with all <,>,& to be converted into <, >, and &. When I am doing transformation, I am getting <,> etc.
    Can anybody suggest me how to do that.
    regards,
    Ranjan

    I don't know of any way to tell the parser to convert is for you, you'll have to replace the characters yourself after you got the string from the parser.
    Aviran
    http://www.aviransplace.com

  • Convertion of XML node to string using Xquery transformation in OSB

    How to convert XML node to string using a built in function using Xquery transformation in OSB? In BPEL we have the Xpath extension function ora:getContentAsString() to do the same.

    fn:bea-serialize() function converts xml node to string. but it assigns namespace prefix in every xml node during the conversion. So is there any function to remove the namespace prefix from XML node using Xquery built in function?

Maybe you are looking for