Conversion of xml string to xml file

To convert xml string to xml file in java i used
Document XMLDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader("<root><main>Title</main></root&g t;")));
But it is showing an error as InputSource cannot be resolved
How to rectify this

I assume you mean there is a compiler error? (It helps if you explain your problem instead of just giving a vague description of it.) It sounds like you have to import InputSource. You do know about the import statements that go at the beginning of a Java class, don't you?

Similar Messages

  • Create Business Partner from XMl String not a file

    Hi,
    Is it possible to create a BP from an XML string. 
    I know about GetBusinessPartnerFromXml(string FileName, int index)
    but I dont want to save the string in a file before creating a BP from it
    Hope there's a method to do that

    Marc,
    This looks like it is a duplicate of this post, so I am closing this thread as it looks like you answered your own question!
    Create Business Partner from XMl String not a file
    Eddy

  • XML string to XML parsing in JCD

    I have stored an XML file as a CLOB in the Oracle DB. While fetching this data into JCD using Oracle OTD, I am getting this CLOB field as a string containing the XML. Now I want to parse this XML string to XML, as I need to map the individual fields to an XSD OTD, which will be my output.
    Kindly suggest a way to achieve this.

    An XSD OTD has an unmarshalFromString() method:
    inputFormat.unmarshalFromString( strData );
    When putting the XML into the CLOB it could be a good idea to wrap an outputstream into a Writer object in order to make certain that the encoding is correct, depending how the data is represented. When retrieving CLOB data using getCharacterStream() you will get a Reader object where the encoding is already given.

  • XML String to XML Parsing

    I have stored an XML file as a CLOB in the Oracle DB. While fetching this data into JCD in JavaCAPS using Oracle OTD, I am getting this CLOB field as a string containing the XML. Now I want to parse this XML string to XML, as I need to map the individual fields to an XSD OTD, which will be my output.
    Kindly suggest a way to achieve this.

    The forum itself is a good starting point : [http://forum.java.sun.com/thread.jspa?forumID=34&threadID=347151] is a result searching for "parse string"...

  • Convert XML string into XML

    Hi All,
    Can you please let me know for any sample code in xslt/java mapping for converting XML string into XML. We use SAP Pi 7.0
    My XML string starts like this
    <?xml version="1.0" encoding="UTF-8" ?> 
    - <ns0:MT_ReceiverFileStructure <namespace>"><Output><?xml version="1.0" encoding="ISO-8859-9"?><?xml-stylesheet type="text/xsl" href="<xsl>"?><Tarih_Date Tarih="11.09.2014" Date="09/11/2014>
       Thanks,
       Pavithra

    Thanks Praveen. It worked.
    However, the xml i have is an extract from a exchange rate URL and it has the reference to a xsl in it as below
    <?xml version="1.0" encoding="ISO-8859-9"?><?xml-stylesheet type="text/xsl" href="<ABC.xsl>"?>.
    So there is an error in sxmb_moni. Is it possible to remove this.

  • How to convert xml String to xml file?

    Hi,
    I got a method that do the following:
    public static String convertXmlToString(String xmlFileName) throws Exception
            File file = new File(xmlFileName);
            FileInputStream insr = new FileInputStream(file);
            byte[] fileBuffer = new byte[(int)file.length()];
            insr.read(fileBuffer);
            insr.close();
            return new String(fileBuffer);
         }The return String is the contents of an existing xml file and one example is shown below:
    <object type="server.WOXReference" id="0"><field name="actualPathObj"><object type="java.lang.String" id="1">C:\restClient\actualXMLObjects\812856084.xml</object></field><field name="object"><object type="server.WOXConstructor" id="2"><field name="className"><object type="java.lang.String" id="3">rec.SimpleRecognizer</object></field><field name="types"><array type="java.lang.String" length="3" id="4"><object type="java.lang.String" id="5">double[].class</object><object type="java.lang.String" id="6">double.class</object><object idref="6" /></array></field><field name="args"><array type="java.lang.Object" length="3" id="7"><array type="double" length="5" id="8">22.33 33.22 33.33 22.0 11.0</array><object type="java.lang.Double" id="9">2.2</object><object type="java.lang.Double" id="10">3.3</object></array></field><field name="retType"><object type="java.lang.String" id="11">act</object></field></object></field></object>
    and I want to change the above string to xml file like below:
    <object type="server.WOXReference" id="0"><field name="actualPathObj"><object type="java.lang.String" id="1">C:\restClient\actualXMLObjects\812856084.xml</object></field><field name="object"><object type="server.WOXConstructor" id="2"><field name="className"><object type="java.lang.String" id="3">rec.SimpleRecognizer</object></field><field name="types"><array type="java.lang.String" length="3" id="4"><object type="java.lang.String" id="5">double[].class</object><object type="java.lang.String" id="6">double.class</object><object idref="6" /></array></field><field name="args"><array type="java.lang.Object" length="3" id="7"><array type="double" length="5" id="8">22.33 33.22 33.33 22.0 11.0</array><object type="java.lang.Double" id="9">2.2</object><object type="java.lang.Double" id="10">3.3</object></array></field><field name="retType"><object type="java.lang.String" id="11">act</object></field></object></field></object>How to I change the above xml String back to xml file again? The appropriate tags must be taken care of like > must be > and < must be <.
    Please advice and can give me some sample code.
    Thanks.

    By writing some code that runs through the file and adds a new line for a new element using these methods here...
    http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/ContentHandler.html
    i.e.
    startDocument()
    startElement(String namespaceURI, String localName, String qName, Attributes atts)
    endDocument()

  • Specify xml string in .java file?

    I have a class A in a .java file where I would like to specify a string which is a xml snippet:
    public class A  {
    public String getXmlString() {
    String myXmlString = "<?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications
          with XML.</description>
       </book>
    </catalog>"
    }But that does not compile. I have tried to use the escape \\ charater for the qoutes and angle brackets but that does not solve the problem. How do I specify a string with xml content in a class?

    georgemc wrote:
    killfinsmiltesn wrote:
    I am new here and read this article and agree with this topic.Thanks to share your thought with us.Sure. I think you're a spamming clown.I agree.
    Spam and spammer blocked (again!)
    db

  • Convert xml string into xml file?

    Hi,
    I got a method that returns an JDom Element object.
    Then I use the JDOM Element toString method() to change it into xml string.
    Now, I need to convert it into actual xml file.
    How to I do it?
    Thanks.

    Hi,
    If you got an xml string, just write it to an ordinary file. Look at FileWriter.
    /Kaj

  • Insert XML String into XML file at a certain position

    Hello,
    I have this xml string:
    <jdbc-driver-params>
    <url>jdbc:oracle:thin:@localhost:1521:mydb</url>
    <driver-name>oracle.jdbc.OracleDriver</driver-name>
    <properties>
    <property>
    <name>user</name>
    <value>myuser</value>
    </property>
    </properties>
    <password-encrypted>myuser</password-encrypted>
    </jdbc-driver-params>and i need to insert it into an xml file at a certain position (as child for a node)
    The xml file looks like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <jdbc-data-source xmlns="http://www.bea.com/ns/weblogic/90"
        xmlns:sec="http://www.bea.com/ns/weblogic/90/security"
        xmlns:wls="http://www.bea.com/ns/weblogic/90/security/wls"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/920 http://www.bea.com/ns/weblogic/920.xsd">
        <name>myDB</name>
        <jdbc-data-source-params>
            <global-transactions-protocol>OnePhaseCommit</global-transactions-protocol>
        </jdbc-data-source-params>
    </jdbc-data-source>And this is how I would like to get it in the end:
    <?xml version='1.0' encoding='UTF-8'?>
    <jdbc-data-source xmlns="http://www.bea.com/ns/weblogic/90" xmlns:sec="http://www.bea.com/ns/weblogic/90/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wls="http://www.bea.com/ns/weblogic/90/security/wls" xsi:schemaLocation="http://www.bea.com/ns/weblogic/920 http://www.bea.com/ns/weblogic/920.xsd">
      <name>mydb</name>
      <jdbc-driver-params>
        <url>jdbc:oracle:thin:@localhost:1521:myDB</url>
        <driver-name>oracle.jdbc.OracleDriver</driver-name>
        <properties>
          <property>
            <name>user</name>
            <value>myuser</value>
          </property>
        </properties>
        <password-encrypted>myuser</password-encrypted>
      </jdbc-driver-params>
      <jdbc-connection-pool-params>
        <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
      </jdbc-connection-pool-params>
      <jdbc-data-source-params>
        <global-transactions-protocol>OnePhaseCommit</global-transactions-protocol>
      </jdbc-data-source-params>
    </jdbc-data-source>So, I obtain the node for the given xml string, the jdbc-data-source node from the xml file and i attempt the appendChild. The problem is that nothing really gets inserted into the xml file.
    Someone, any idea?
    My code:
    try
                   DOMParser parser = new DOMParser();
                   parser.parse(xmlFileName);
                   Document doc = parser.getDocument(); // the document we will insert node into
                   Node root = doc.getElementsByTagName("jdbc-data-source").item(0);
                   //get XML string node
                   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                   DocumentBuilder db = factory.newDocumentBuilder();
                   InputSource inStream = new InputSource();
                   inStream.setCharacterStream(new StringReader(xmlString));
                   Document doc4XMLString = db.parse(inStream);
                   Node rootXMLString = doc4XMLString.getElementsByTagName("jdbc-driver-params").item(0);
                   root.appendChild(doc.importNode(rootXMLString, true));
                   return true;
              catch (Exception e)
                   e.printStackTrace();
              }Thanks,
    johnny
    Edited by: smeag0l on Aug 6, 2008 5:08 PM
    Edited by: smeag0l on Aug 6, 2008 5:43 PM

    Well, you helped me again :-) Thank you very much!
    Here is how the code should look:
                            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                   DocumentBuilder db4XMLFile = factory.newDocumentBuilder();
                   Document doc4XMLFile = db4XMLFile.parse(xmlFileName);
                   Node root4XMLFile = doc4XMLFile.getElementsByTagName(rootNodeName).item(0);
                   DocumentBuilder db4XMLString = factory.newDocumentBuilder();
                   InputSource inStream = new InputSource();
                   inStream.setCharacterStream(new StringReader(xmlString));
                   Document doc4XMLString = db4XMLString.parse(inStream);
                   Node root4XMLString = doc4XMLString.getElementsByTagName(xmlStringNodeName).item(0);
                   root4XMLFile.appendChild(doc4XMLFile.importNode(root4XMLString, true));
                   File file = new File(xmlFileName);
                            Result result = new StreamResult(file.toURI().getPath());
                   Source source = new DOMSource(doc4XMLFile);
                   Transformer xformer = TransformerFactory.newInstance().newTransformer();
                      xformer.transform(source, result);

  • REG: WRITE XML STRING INTO XML FILE

    Hi All, I have a XML FILE on PC. I get a xml string from my server.
    My Task: I want to append the xml string coming from the server into the existing xml file at a particular Node. Given below is my code :
    String xmlString = baos.toString();
                      DocumentBuilder db4XMLString = docBuilderFactory.newDocumentBuilder();
                      InputSource inStream = new InputSource();
                      inStream.setCharacterStream(new StringReader(xmlString));
                      Document doc4XMLString = db4XMLString.parse(inStream);
                      Node root4XMLString = doc4XMLString.getElementsByTagName("epg").item(0);
                      doc.appendChild(doc.importNode(root4XMLString, true));
                      File file = new File(Constant.XMLFILEPATH);
                      Result result = new StreamResult(file.toURI().getPath());
                      Source source = new DOMSource(doc);
                      Transformer xformer = TransformerFactory.newInstance().newTransformer();
                      xformer.transform(source, result);But, wen i run my application I get following exception:
    The markup in the document following the root element must be well-formed.
    org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    Is that my XML string is not well formed. Is it so then please let me how to convert my xml string into well formed XML data.
    Please help me out.
    Thanking You.

    There are many reasons why xmlString could be not well formed. There is no magic recipe for making it well formed: just look at it and see why it is not parseable.
    Note that this line:
    String xmlString = baos.toString();can be problematic if you have non ASCII-7 characters in your XML document. baos could then contain a byte representation of these characters in one particular encoding, but, since you use the .toString() method that relies on the default encoding of your system, you can end up with a mismatch and garbage in your XML.

  • Writing a large xml string to a file

    Hi,
    I get a large xml file, into a String (It might be around 8 MB also). After this, I need to write this to a file. Which java method I can use for the same.
    I am getting confused by the various classes for writing a file.
    I want to use the best, efficient way of doing this. Can any one please help?
    public static void writeStringToFile( String fileName, String content,
              boolean append )
              throws FileNotFoundException, IOException
              FileOutputStream fos = null;
              File file = new File (fileName);
              try {
    //++  fos = new FileOutputStream( file);     
          fos = new FileOutputStream( file, append );                 //++
                   fos.write(getBytes(content));
              } catch (FileNotFoundException fne) {
                   throw fne;
              } catch (IOException ioe) {
                   throw ioe;
              } finally {
                   try {
                   if(fos != null)
                        fos.close();
                   } catch (Exception _ex) {
         }

    hi_all wrote:
    But is there any any particular reason for using the encoding specific to xml?Yes, because that's what the XML specification says you're supposed to do. If you don't do that then you may produce malformed XML.
    Will there be any perf degrade or truncation of the large string, while writing to the file, if we use,
    out.write(sigUpdateString);Compared to what? You're just writing the data out to a file, the quickest way to do that is to just write the data out. The two things you should do are: (1) use a BufferedWriter (2) stop obsessing about performance.

  • From XML string to XML.

    Hi Guys,
             I have scenario wherein there is whole XML in a single element and i want to retrieve it to target XML structure. How do i do it?
    Source message is:
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    - <SubmitBatchPwResponse xmlns="http://AltInn.no/webservices/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <SubmitBatchPwResult><?xml version="1.0"?> <b><DataBatchInReceipt schemaVersion="1.0" batchId="9e7d6e34-60b3-4c42-89f8-dce662913d3f" enterpriseSystemId="826" status="OK"><DataUnitInReceipt sendersReference="fd1fc4b6-ffee-47dd-aeee-d3d57b1d6b2e" submissionReference="{A07CD759-BC7C-4AC8-92BD-EE98D2CFD363}" status="OK" timeReceived="2006-10-11T14:16:14"><Message><MessageEntry type="Default" logged="2006-10-11T14:16:15">Sendt til Altinn</MessageEntry></Message></DataUnitInReceipt></DataBatchInReceipt></b></SubmitBatchPwResult>
      </SubmitBatchPwResponse>
    The target XSD is:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="DataUnitInReceipt">
    <xsd:annotation>
    <xsd:documentation>
    Status information per dataunit or attachment in a batch.
    </xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="Message" minOccurs="0" form="qualified">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="MessageEntry" minOccurs="0" maxOccurs="unbounded" form="qualified">
    <xsd:annotation>
    <xsd:documentation>
    Additional information.This is usually a textual representation, but is XML representation for ValidationError messages
    </xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
    <xsd:sequence>
    <xsd:any namespace="##any" processContents="skip" />
    </xsd:sequence>
    <xsd:attribute name="type" use="required">
    <xsd:annotation />
    <xsd:simpleType>
    <xsd:restriction base="xsd:string">
    <xsd:enumeration value="Default" />
    <xsd:enumeration value="VersionError" />
    <xsd:enumeration value="GenericError" />
    <xsd:enumeration value="Recipient" />
    <xsd:enumeration value="AttachmentNotAllowedError" />
    <xsd:enumeration value="ValidationError" />
    <xsd:enumeration value="SubFormNotAllowedError" />
    <xsd:enumeration value="KID" />
    <xsd:enumeration value="EnterpriseSigningError" />
    <xsd:enumeration value="Received" />
    <xsd:enumeration value="SignedByParticipant" />
    <xsd:enumeration value="SignedByAccountant" />
    <xsd:enumeration value="DeletedWorkFlow" />
    <xsd:enumeration value="AddedWorkFlow" />
    <xsd:enumeration value="Archived" />
    </xsd:restriction>
    </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="logged" type="xsd:dateTime" use="required">
    <xsd:annotation />
    </xsd:attribute>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="sendersReference" use="required">
    <xsd:annotation>
    <xsd:documentation>
    Unique id for the data on the original senders side. Must be unique within the system id.
    </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleType>
    <xsd:restriction base="xsd:string">
    <xsd:minLength value="1" />
    </xsd:restriction>
    </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="submissionReference" use="required">
    <xsd:annotation>
    <xsd:documentation>
    This is the identification of the initial storage of the dataunit or attachment. This is globally unique within AltInn
    </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleType>
    <xsd:restriction base="xsd:string">
    <xsd:minLength value="1" />
    </xsd:restriction>
    </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="workflowReference" type="xsd:string">
    <xsd:annotation>
    <xsd:documentation>
    Identification of the dataunit or attachment when submittet to the worflow module. This is globally unique within AltInn.
    </xsd:documentation>
    </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="archiveReference" type="xsd:string">
    <xsd:annotation>
    <xsd:documentation>
    Identification of the dataunit or attachment when submittet to the archive. This is globally unique within AltInn.
    </xsd:documentation>
    </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="receiversReference" type="xsd:string">
    <xsd:annotation>
    <xsd:documentation>
    Identification of the dataunit or attachment from the reciever of the data. The uniqueness of this reference cannot be guaranteed.
    </xsd:documentation>
    </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="status" type="xsd:string" use="required">
    <xsd:annotation>
    <xsd:documentation>
    Status of the dataunit. The possible statuses are given in the documentation. The status may be supplemented by the message element.
    </xsd:documentation>
    </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="timeReceived" use="required" />
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="DataBatchInReceipt">
    <xsd:annotation>
    <xsd:documentation>
    Container for the receipts in a batch.
    </xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="Message" type="xsd:string" minOccurs="0" form="qualified">
    <xsd:annotation>
    <xsd:documentation>
    Additional information accompanying the status
    </xsd:documentation>
    </xsd:annotation>
    </xsd:element>
    <xsd:element ref="DataUnitInReceipt" maxOccurs="unbounded" />
    </xsd:sequence>
    <xsd:attribute name="schemaVersion" type="xsd:decimal" use="required">
    <xsd:annotation>
    <xsd:documentation>
    Corresponding schema version
    </xsd:documentation>
    </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="batchId" use="required">
    <xsd:annotation>
    <xsd:documentation>
    Unique id of the batch within the scope of the enterpriseSystemId.
    </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleType>
    <xsd:restriction base="xsd:string">
    <xsd:minLength value="1" />
    <xsd:maxLength value="50" />
    </xsd:restriction>
    </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="enterpriseSystemId" type="xsd:int" use="required">
    <xsd:annotation>
    <xsd:documentation>
    Unique id of the system sending data. This id is obtained from registering the system with AltInn
    </xsd:documentation>
    </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="status" use="required">
    <xsd:annotation>
    <xsd:documentation>
    Status of the batch as a whole. The status may be Ok even though individual statuses of dataunits are not Ok. The possible statuses are given in the documentation. The status may be supplemented by the message element.
    </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleType>
    <xsd:restriction base="xsd:string">
    <xsd:enumeration value="OK" />
    <xsd:enumeration value="AccessDenied" />
    <xsd:enumeration value="FormatError" />
    <xsd:enumeration value="ValidationError" />
    <xsd:enumeration value="AllreadyExists" />
    <xsd:enumeration value="DoesNotExist" />
    <xsd:enumeration value="GenericError" />
    <xsd:enumeration value="InternalError" />
    <xsd:enumeration value="SigningAuthenticationError" />
    <xsd:enumeration value="SigningAuthorisationError" />
    <xsd:enumeration value="SigningOptionError" />
    <xsd:enumeration value="VirusDetected" />
    <xsd:enumeration value="DuplicateDataUnit" />
    <xsd:enumeration value="AuthorizationFailed" />
    </xsd:restriction>
    </xsd:simpleType>
    </xsd:attribute>
    </xsd:complexType>
    </xsd:element>
    </xsd:schema>
    Pls advice on how to go about this.
    Regards
    Ashish

    Hi Ashish,
    As usual use our XSLT mapping to do it, its pretty simple using XSLT. But you can also achieve this with other mappings but XSLT is simple for this kind of job. Can you post the target structure in XML format. So that it will be easy for us to help you in XSLT.
    Thanks,
    Prakash

  • Extracting elements from an xml string - org.apache.xerces.dom.DeferredText

    Hello all,
    I am new to xml, and I thought I had a handle on things until I got this problem...I am getting an xml string from the body of an e-mail message, and then I am trying to extract elements out of it. Here is an example xml string:
    <?xml version="1.0" encoding="UTF-8"?>
    <filterRoot>
       <filter action="MOVE" bool="AND" name="My Saved Filter" target="Deleted Items">
          <condition attribute="TO" bool="AND" contains="CONTAINS">
             <value>[email protected]</value>
             <value>[email protected]</value>
             <value>[email protected]</value>
             <value>[email protected]</value>
             <value>[email protected]</value>
          </condition>
       </filter>
    </filterRoot>I am trying to extract the <filter> element out and store it into a Vector of Elements (called, not surprisingly, filters). However, I am getting a class cast exception:
    java.lang.ClassCastException: org.apache.xerces.dom.DeferredTextImplIt is being called from where I trying to extract the <filter> in this way:
            filterRoot = doc.getDocumentElement(); // get topmost element
            NodeList list = filterRoot.getChildNodes();
            Vector newFilters = new Vector();
            debug("There are "+list.getLength()+" filters in document");
            for(int i=0; i<list.getLength(); i++) {
                Node n = list.item(i);
                debug("Node "+i+" getNodeValue() is "+n.getNodeValue());
                Element temp = (Element)n;
                newFilters.add(temp);
            }Perhaps my question is, how do I correctly get hold of the <filter> node so that I may cast it as an Element?
    thanks,
    Riz

    Yes, I already knew that it is not a bug.
    But, I got next step problem.
    I put "false" to "include-ignorable-whitespace" feature in xerces parser.
    But, I still found unnecessary TextNodes in my parser object.
    Feature link : http://xerces.apache.org/xerces-j/features.html.
    I use xerces-2_8_0.
    DOMParser parser = new DOMParser();
    parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
    parser.parse(inputSource);
    document = ps.getDocument();
    System.out.println(document.getDocumentElement().getChildNodes().length()); // still wrong lengthIs tehre any example of usage this feature?
    What is default defination of white-space "\n "(enter with one space) or " "(juz white space) or something else?
    Thanks,

  • Error after transforming xml string

    I have an XML string that I have to parse and update (transformer)
              String ji = "<book><person><first>p</first><last>m</last><age>22</age></person><person><first>b</first><last>g</last><age>46</age></person><person><first>s</first><last>j</last><age>40</age></person></book>";
              ByteArrayInputStream str = new ByteArrayInputStream(ji.getBytes());
                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                   DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
                   Document doc = docBuilder.parse(str);
                   doc.getDocumentElement().normalize();
                   str.reset();
                   ByteArrayOutputStream bos = new ByteArrayOutputStream();
                   NodeList listOfPersons = doc.getElementsByTagName("person");
                             int total = listOfPersons.getLength();
                             for(int i = 0; i < total; i++)
                                  Node firstpersonnode = listOfPersons.item(i);
                                  if(firstpersonnode.getNodeType() == Node.ELEMENT_NODE)
                                       Element firstPersonElement = (Element)firstpersonnode;
                                       NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                                       Element firstNameElement = (Element)firstNameList.item(0);
                                       NodeList textFNList = firstNameElement.getChildNodes();
                                       Text t = (Text) textFNList.item(0);
                                       String p = t.toString();
                                       System.out.println("First Name : " +p);
                                       t.setData("sdasdsa");
                                       //String q = t.toString();
                                       //response.write("First Name : " +q);
                                       NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                                       Element lastNameElement = (Element)lastNameList.item(0);
                                       NodeList textLNList = lastNameElement.getChildNodes();
                                       System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
                                       NodeList ageList = firstPersonElement.getElementsByTagName("age");
                                       Element ageElement = (Element)ageList.item(0);
                                       NodeList textAgeList = ageElement.getChildNodes();
                                       System.out.println("Age : " + ((Node)textAgeList.item(0)).getNodeValue().trim());
    I then write the ByteArrayOutputStream to an OutputStream becoz I need it in that format for the Transformer.
    oos = bos;
    str.reset();
    Source source = new DOMSource(doc);
    Result res = new StreamResult(oos);
    Transformer x = TransformerFactory.newInstance().newTransformer();
    x.transform(source,res);
    System.out.println("now the string is = "+oos.toString());
    Which Transforms it:
    Now my output is the Old XML string + some xml declaration + the Updated XML string.
    First Name : p
    Last Name : m
    Age : 22
    First Name : b
    Last Name : g
    Age : 46
    First Name : s
    Last Name : j
    Age : 40
    now the string is = <book><person><first>p</first><last>m</last><age>22</age></person><person><first>b</first><last>g</last><age>46</age></person><person><first>s</first><last>j</last><age>40</age></person></book><?xml version="1.0" encoding="utf-8"?>
    <book><person><first>sdasdsa</first><last>m</last><age>22</age></person><person><first>sdasdsa</first><last>g</last><age>46</age></person><person><first>sdasdsa</first><last>j</last><age>40</age></person></book>
    I only need to update the string

    Clap... here is the compilable code complete:
    Tried to make it shorter...:
    public class TestXML {
         public static void main(String[] args) {
              String ji = "<book><person><first>p</first><last>m</last><age>22</age></person><person><first>b</first><last>g</last><age>46</age></person><person><first>s</first><last>j</last><age>40</age></person></book>";
              ByteArrayInputStream str = new ByteArrayInputStream(ji.getBytes());
              ByteArrayOutputStream os=null;
              OutputStream oos = null;
              try
                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                   DocumentBuilder docBuilder =  documentBuilderFactory.newDocumentBuilder();
                   Document doc = docBuilder.parse(str);
                   doc.getDocumentElement().normalize();
                   str.reset();
                   ByteArrayOutputStream bos = new ByteArrayOutputStream();
                   NodeList listOfPersons = doc.getElementsByTagName("person");
                             int total = listOfPersons.getLength();
                             for(int i = 0; i < total; i++)
                                  Node firstpersonnode = listOfPersons.item(i);
                                  if(firstpersonnode.getNodeType() == Node.ELEMENT_NODE)
                                       Element firstPersonElement =  (Element)firstpersonnode;
                                       NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                                       Element firstNameElement = (Element)firstNameList.item(0);
                                       NodeList textFNList = firstNameElement.getChildNodes();
                                       Text t = (Text) textFNList.item(0);
                                       String p = t.toString();
                                       System.out.println("First Name : " +p);
                                       t.setData("sdasdsa");
                                                                     NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                                       Element lastNameElement = (Element)lastNameList.item(0);
                                       NodeList textLNList = lastNameElement.getChildNodes();
                                       System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
                                       NodeList ageList = firstPersonElement.getElementsByTagName("age");
                                       Element ageElement = (Element)ageList.item(0);
                                       NodeList textAgeList = ageElement.getChildNodes();
                                       System.out.println("Age : " +   ((Node)textAgeList.item(0)).getNodeValue().trim());
                   byte b;
                   while ((b =(byte) str.read()) != -1) {
                        bos.write(b);
                   oos = bos;
                                            str.reset();
                             Source source = new DOMSource(doc);
                             Result res = new StreamResult(oos);
                             Transformer x =  TransformerFactory.newInstance().newTransformer();
                             x.transform(source,res);
                             System.out.println("now the string is = "+oos.toString());
         catch(Exception e){System.out.println(e);}
    }The o/p is as I mentioned in my first post
    Message was edited by:
    PremPrem
    Message was edited by:
    PremPrem

  • Error transforming XML string

    I have an XML string that I have to parse and update (transformer)
              String ji = "<book><person><first>p</first><last>m</last><age>22</age></person><person><first>b</first><last>g</last><age>46</age></person><person><first>s</first><last>j</last><age>40</age></person></book>";
              ByteArrayInputStream str = new ByteArrayInputStream(ji.getBytes());
                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                   DocumentBuilder docBuilder =  documentBuilderFactory.newDocumentBuilder();
                   Document doc = docBuilder.parse(str);
                   doc.getDocumentElement().normalize();
                   str.reset();
                   ByteArrayOutputStream bos = new ByteArrayOutputStream();
                   NodeList listOfPersons = doc.getElementsByTagName("person");
                             int total = listOfPersons.getLength();
                             for(int i = 0; i < total; i++)
                                  Node firstpersonnode = listOfPersons.item(i);
                                  if(firstpersonnode.getNodeType() == Node.ELEMENT_NODE)
                                       Element firstPersonElement =  (Element)firstpersonnode;
                                       NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                                       Element firstNameElement = (Element)firstNameList.item(0);
                                       NodeList textFNList = firstNameElement.getChildNodes();
                                       Text t = (Text) textFNList.item(0);
                                       String p = t.toString();
                                       System.out.println("First Name : " +p);
                                       t.setData("sdasdsa");
                                       //String q = t.toString();
                                       //response.write("First Name : " +q);
                                       NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                                       Element lastNameElement = (Element)lastNameList.item(0);
                                       NodeList textLNList = lastNameElement.getChildNodes();
                                       System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
                                       NodeList ageList = firstPersonElement.getElementsByTagName("age");
                                       Element ageElement = (Element)ageList.item(0);
                                       NodeList textAgeList = ageElement.getChildNodes();
                                       System.out.println("Age : " +   ((Node)textAgeList.item(0)).getNodeValue().trim());
                             }I then write the ByteArrayOutputStream to an OutputStream becoz I need it in that format for the Transformer.
    oos = bos;
    str.reset();
                             Source source = new DOMSource(doc);
                             Result res = new StreamResult(oos);
                             Transformer x = TransformerFactory.newInstance().newTransformer();
                             x.transform(source,res);
                             System.out.println("now the string is = "+oos.toString());
    Which Transforms it:
    Now my output is the Old XML string + some xml declaration + the Updated XML string.
    First Name : p
    Last Name : m
    Age : 22
    First Name : b
    Last Name : g
    Age : 46
    First Name : s
    Last Name : j
    Age : 40
    now the string is = <book><person><first>p</first><last>m</last><age>22</age></person><person><first>b</first><last>g</last><age>46</age></person><person><first>s</first><last>j</last><age>40</age></person></book><?xml version="1.0" encoding="utf-8"?>
    <book><person><first>sdasdsa</first><last>m</last><age>22</age></person><person><first>sdasdsa</first><last>g</last><age>46</age></person><person><first>sdasdsa</first><last>j</last><age>40</age></person></book>I only need to update the string

    I have an XML string that I have to parse and update (transformer)
              String ji = "<book><person><first>p</first><last>m</last><age>22</age></person><person><first>b</first><last>g</last><age>46</age></person><person><first>s</first><last>j</last><age>40</age></person></book>";
              ByteArrayInputStream str = new ByteArrayInputStream(ji.getBytes());
                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                   DocumentBuilder docBuilder =  documentBuilderFactory.newDocumentBuilder();
                   Document doc = docBuilder.parse(str);
                   doc.getDocumentElement().normalize();
                   str.reset();
                   ByteArrayOutputStream bos = new ByteArrayOutputStream();
                   NodeList listOfPersons = doc.getElementsByTagName("person");
                             int total = listOfPersons.getLength();
                             for(int i = 0; i < total; i++)
                                  Node firstpersonnode = listOfPersons.item(i);
                                  if(firstpersonnode.getNodeType() == Node.ELEMENT_NODE)
                                       Element firstPersonElement =  (Element)firstpersonnode;
                                       NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                                       Element firstNameElement = (Element)firstNameList.item(0);
                                       NodeList textFNList = firstNameElement.getChildNodes();
                                       Text t = (Text) textFNList.item(0);
                                       String p = t.toString();
                                       System.out.println("First Name : " +p);
                                       t.setData("sdasdsa");
                                       //String q = t.toString();
                                       //response.write("First Name : " +q);
                                       NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                                       Element lastNameElement = (Element)lastNameList.item(0);
                                       NodeList textLNList = lastNameElement.getChildNodes();
                                       System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
                                       NodeList ageList = firstPersonElement.getElementsByTagName("age");
                                       Element ageElement = (Element)ageList.item(0);
                                       NodeList textAgeList = ageElement.getChildNodes();
                                       System.out.println("Age : " +   ((Node)textAgeList.item(0)).getNodeValue().trim());
                             }I then write the ByteArrayOutputStream to an OutputStream becoz I need it in that format for the Transformer.
    oos = bos;
    str.reset();
                             Source source = new DOMSource(doc);
                             Result res = new StreamResult(oos);
                             Transformer x = TransformerFactory.newInstance().newTransformer();
                             x.transform(source,res);
                             System.out.println("now the string is = "+oos.toString());
    Which Transforms it:
    Now my output is the Old XML string + some xml declaration + the Updated XML string.
    First Name : p
    Last Name : m
    Age : 22
    First Name : b
    Last Name : g
    Age : 46
    First Name : s
    Last Name : j
    Age : 40
    now the string is = <book><person><first>p</first><last>m</last><age>22</age></person><person><first>b</first><last>g</last><age>46</age></person><person><first>s</first><last>j</last><age>40</age></person></book><?xml version="1.0" encoding="utf-8"?>
    <book><person><first>sdasdsa</first><last>m</last><age>22</age></person><person><first>sdasdsa</first><last>g</last><age>46</age></person><person><first>sdasdsa</first><last>j</last><age>40</age></person></book>I only need to update the string

Maybe you are looking for

  • Exception Handling in Inbound Synchronous ABAP Proxy

    Hi All, I am implementing inbound synchronous proxy which has inbound ,outbound and fault message ( same of SAP-PI). I am calling a FM inside inside the method. All the business logic is coded in the FM which also handles the exception. The exception

  • Dot1x clients not authenticated after reload

    Hi all, I have a switch setup with dynamic vlan assignment. Everything works fine until the switch is rebooted. Then none of the pc's are authenticated anymore. I have to do a shut/no shut of all the user ports to start the re-authentication of the p

  • Client-Copy form ecc 6.0 ehp 1 to ecc 6.0 ehp4

    Hello Experts, I would like to copy an entire Client from one system to another but the systems are on different ehp-levels. The source-system is on ehp1 level while the target-system is on ehp4 level. Are there any known problems that i should be aw

  • X:panelTabbedPane + Save button troubles

    I make changes into one of tabs. After this I click on Save button and get error message saying that fields on other tabs are empty. To avoid this error, I have to switch to ALL the tabs, making them active and initializing their fields and only then

  • An Obscure Problem

    So, I have an Ipod Video. I was surfing the web when I saw that apple had come out with updated firmware, so I dived right in. I updated my iPod and the message come up on my computer that it had transfered the firmware and that the iPod had to load.