Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2004, 07:34 AM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Append child problem org.w3c.dom

HELLO
i m converting csvfile to xml. using org andcom.Ostermiller.util.CSVParser i got out is like that.. but </RECORD> element not append properly
,when end of line only that childis append
help me

<CSV2XML>
    <RECORD>
        <Ticketid>DC2000000708378</Ticketid>
        <Status>ss</Status>
        <Severity>Relevant</Severity>
        <Symptom>DDOS Stacheldraht client check gag</Symptom>
        <Sentryid>trii-001</Sentryid>
        <GatewayMessages>2</GatewayMessages>
        <Ticketid>DC2000000708383</Ticketid>
        <Status>Resolved</Status>
        <Severity>Relevant</Severity>
        <Symptom>named-probe-authors: Snort IDS480</Symptom>
        <Sentryid>trii-001</Sentryid>
        <GatewayMessages>3</GatewayMessages>
        <Ticketid>DC2000000708384</Ticketid>
        <Status>Resolved</Status>
        <Severity>Relevant</Severity>
        <Symptom>dns_named-probe-version: Snort</Symptom>
        <Sentryid>trii-001</Sentryid>
        <GatewayMessages>2</GatewayMessages>
    </RECORD>
</CSV2XML>

my java file






import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import com.Ostermiller.util.CSVParser;

public class CSVReader {

 // Protected Properties
    protected DocumentBuilderFactory domFactory = null;
    protected DocumentBuilder domBuilder = null;


    protected String StrValues;
    protected String szNewString;

     public CSVReader()
    {
        try
        {
            domFactory = DocumentBuilderFactory.newInstance();
            domBuilder = domFactory.newDocumentBuilder();
        }
        catch(FactoryConfigurationError exp)
        {
             System.err.println(exp.toString());
        }
        catch(ParserConfigurationException exp)
        {
             System.err.println(exp.toString());
        }
        catch(Exception exp)
        {
            System.err.println(exp.toString());
        }
    }


    //
    public int convertFile(String csvFileName, String xmlFileName)
    {

        int rowsCount = -1;
        try
        {
             Document newDoc = domBuilder.newDocument();

            // Root element
            Element rootElement = newDoc.createElement("CSV2XML");
            newDoc.appendChild(rootElement);

            FileReader fReader = new FileReader(csvFileName);
            CSVParser Strbuffer = new CSVParser(fReader);
            Strbuffer.setCommentStart("#;!");
            Strbuffer.setEscapes("nrtf", "\n\r\t\f");

            String[] strValues = null;
            String[] strTitle =null;




            // Assumption: first line in CSV file is column/field names
            // As the column names are used to name the elements in the XML file,
            // avoid using any other characters not suitable for XML element naming

            if ((strTitle =Strbuffer.getLine()) != null)
            {

              Element rowElement = newDoc.createElement("RECORD");

                // Now we know the columns, now read data row lines

              while((strValues = Strbuffer.getLine()) != null)
               {
                  System.out.println(strValues.length);
                for(int i=0;i<strTitle.length;i++)
                {
                  String Header = strTitle[i];
                  String Value = strValues[i];
                    try
                    {
                      Element curElement = newDoc.createElement(Header);
                      curElement.appendChild(newDoc.createTextNode(Value ));
                      rowElement.appendChild(curElement);
                    // System.out.println("***** "+"**"+Header+"**"+rowElement.appendChild(curEleme nt).getNodeName());
                    }
                    catch(Exception exp)
                    {
                    System.out.println(exp +"4");
                    }
                }

                 // System.out.println("***** **"+rootElement.appendChild(rowElement).getNodeVal ue());
                  rootElement.appendChild(rowElement);
                  rowsCount++;
               }

            }

            Strbuffer.close();
            fReader.close();

            // Save the document to the disk file
            TransformerFactory tranFactory = TransformerFactory.newInstance();
            Transformer aTransformer = tranFactory.newTransformer();

            Source src = new DOMSource(newDoc);
            Result dest = new StreamResult(new File(xmlFileName));
            aTransformer.transform(src, dest);

            rowsCount++;
        }

        catch(Exception exp)
        {
            System.err.println(exp.toString());
        }

        return rowsCount;
    }

   public static void main(String args[])
    {
        try
        {
            if(args.length != 2)
            {
                System.out.println("Usage: java CSV2XML <inputCSVFile> <outputXMLFile>");
                return;
            }
        }
        catch(Exception exp)
        {
            System.err.println(exp.toString());
        }

        try
        {
            CSVReader csvConverter = new CSVReader();
            int rowsCount = csvConverter.convertFile(args[0], args[1]);

            if(rowsCount >= 0)
            {
                System.out.println("CSV File '" + args[0] +
                        "' successfully converted to XML File '"+ args[1] + "'\n" +
                        "(" + String.valueOf(rowsCount) + " rows)");
            }
            else
            {
                System.out.println("Error while converting input CSV File '" + args[0] +
                        "' to output XML File '"+ args[1] + "'");
            }
        }
        catch(Exception exp)
        {
            System.err.println(exp.toString());
        }
    }
}

Shankar sam
India

Shankar sam
India





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Problem in writing within "" by DOM. Somesh XSLT 1 March 5th, 2007 07:27 AM
curious problem with W3C validator spineynorman HTML Code Clinic 1 April 3rd, 2006 08:20 PM
problem with document.location and w3c holdmykidney Javascript 2 May 3rd, 2005 07:41 PM
Help append Child problem vs_shankar2000 Apache Tomcat 0 December 7th, 2004 02:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.