Results 1 to 4 of 4

Thread: Converting CDATA Node into Text Node

  1. #1
    Join Date
    Jan 2009
    Posts
    65

    Converting CDATA Node into Text Node

    Hi, I want to know how to convert CDATA Nodes into Text Nodes While Parsing an XML File? Can anyone help me? I am waiting for your reply. Please help me.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Converting CDATA Node into Text Node

    Hi, for converting CDATA node into text you can use following code in xml. I think this will work for you. Just make use of it. It will help you.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!--     Information  -->
    <Company>
        <Employee Id="Tech-001">
            <CompanyName>Techarena</CompanyName>
            <City>dont</City>>
            <name>my name</name>
            <Phoneno>1234567890</Phoneno>
           <!-- CDATA node -->
           <Doj>techarena
            <![CDATA[techarena.in <"!@#$%'^&*()> is in my name]]>
            Techarena.in
            </Doj>
        </Employee>
    </Company>

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Converting CDATA Node into Text Node

    Hi, to Convert CDATA Node into Text Node, I have got a java code; which works perfectly as we want. You can use that code to do what you want. So, just use the code as below:
    Code:
    import java.io.*;
    import org.w3c.dom.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    
    public class CDATANodesTOTextNodes 
    {
        public static void main(String[] args) throws Exception 
    {
            File first = new File("Cdata.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setCoalescing(true);
            DocumentBuilder dbuild  = dbf.newDocumentBuilder();
            Document doc = dbuild.parse(first);
            new ConvertingCDATANodesTextNodes().CDATAtextnode(doc);
    }
    public void CDATAtextnode(Document doc) throws Exception 
    {
            TransformerFactory tfactory = TransformerFactory.newInstance();
            Transformer transformer = tfactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            StringWriter swriter = new StringWriter();
            StreamResult result = new StreamResult(swriter);
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);
            String xmlfile = swriter.toString();
            System.out.println(xmlfile);
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Converting CDATA Node into Text Node

    Hi, you want to convert cdata node into text node, right? It is very simple with the use of java. You need to use the following small code to get the solution and use it.

    Code:
    import java.io.File;
    
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    
    public class Main 
    {
      public static void main(String[] args) throws Exception
    {
        DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
        dbfactory.setCoalescing(true);
    
        Document doc = dbfactory.newDocumentBuilder().parse(new File("infilename.xml"));
    
      }
    }

Similar Threads

  1. Adding a Node to a DOM Document
    By Level8 in forum Software Development
    Replies: 4
    Last Post: 02-03-2010, 10:24 AM
  2. Preference Node in Java
    By Aakarshan.d in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 09:03 PM
  3. How to add an Image to a parent node in a treeview?
    By Casie in forum Software Development
    Replies: 4
    Last Post: 27-01-2010, 05:02 PM
  4. Invalid Node Structure in MACbook Pro
    By Panchu in forum Operating Systems
    Replies: 5
    Last Post: 06-01-2010, 08:02 PM
  5. can not access node on network
    By spikeofdoom in forum Networking & Security
    Replies: 2
    Last Post: 23-12-2008, 11:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,711,269.74307 seconds with 16 queries