Flat file to XML format


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Flat file to XML format

  1. #1
    Join Date
    Mar 2003
    Posts
    6

    Question Flat file to XML format

    Hi guys

    I have a question. I have a java program that produces a lot of output I would like to save in XML format instead of .txt format. What should I use or is there software that already do it for me? Are there classses already out there that do this? Any help would be great.

    Thanks in advance.

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    This really depends upon how deep into XML you want to get.

    However, the quite and easy (and perhaps just a little bit dirty) solution is to overload the toString() method of you objects to return an XML representation of that object.

    Code:
      public class Catalogue {
        private String title;
        private List books;
        ...
        public String toString() {
          StringBuffer sb = new StringBuffer();
          sb.append("<catalogue title=\"" + title + "\">");
          Iterator i = books.iterator();
          while(i.hasNext()) {
             Book b = (Book)i.next();
             sb.append(b.toString());
          }
          sb.append("</catalogue>");
          return sb.toString();
        }
      }
    
      public class Book {
         private int ISBN;
         private Person author;
         ...
    
         public String toString() {
          StringBuffer sb = new StringBuffer();
          sb.append("<book isbn=\"" + ISBN + "\");
          ....
          return sb.toString();
        }
      }
    So, you just call 'toString()' on the top-most object. It will return some XML which you then write to a file.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Mar 2003
    Posts
    6
    Thanks, is there any imports that I have to download or any parsers? I have a big file around 400+ lines.

  4. #4
    Join Date
    Mar 2003
    Posts
    84
    I'm not following your question. why don't you just post your code and explain what you are trying to do with it a little more.
    I'm surprised more of you people don't get hit by cars.

  5. #5
    Join Date
    Mar 2003
    Posts
    6
    Ok here is my code:

    import java.util.Random;
    import com.objectplanet.chart.*;
    import java.awt.*;
    import java.io.*;

    public class ProjectCalculation extends Frame
    {
    private BarChart chart = new BarChart(6);
    static boolean isStandardOutput = false;
    static Writer outStream;

    public static void main(String[] args)
    {
    ProjectCalculation pc = new ProjectCalculation();

    double yArray[] = new double[400];
    double a, b, c, d, angle;
    int initialConstant = 6;
    int i;

    for(int j = 0; j < 400; j++)
    {
    for(i = 1; i < 11; i++)
    {
    a = pc.getRandomNumber();
    b = pc.getRandomNumber();
    c = pc.getRandomNumber();
    d = pc.getRandomNumber();
    angle = pc.getAngle();
    yArray[j] += (c/d) + ((initialConstant * d - c) /
    (d * Math.cos(angle))) *
    (Math.cos(Math.sqrt(a*c) * j - angle));

    // assign double[] to double
    double lg = yArray[j];

    //print("yArray is : " + yArray[j] + " A: " + a + " B " + b + " C " + c + " D "+ d + " Angle " + angle );
    debug(0, " yArray is : " + yArray[j] + "\n" + " A: " + a + "\n" + " B " + b + "\n" + " C " + c + "\n" +" D "+ d + "\n" +" Angle " + angle );
    if(j == 399)
    {
    pc.display(lg,a,b,c,d,angle);
    }
    }
    }
    }
    //method returns a random number between 1.00 and 2.00
    public double getRandomNumber()
    {
    return Math.random() + 1;
    }

    //method returns a value between 0 and 2Pi, except 0.5Pi
    public double getAngle()
    {
    double result = (Math.PI * 2) * Math.random();
    if(result == Math.PI * 0.5)
    {
    System.exit(1);
    }
    else
    {
    }
    //print("Result : " + result);
    return result;
    }

    public void display(double arr,double a,double b, double c, double d, double ang)
    {

    NonFlickerPanel p = new NonFlickerPanel();
    p.add("Center", chart);
    add(p);

    String[] str = new String[] {"Array","A", "B", "C", "D", "Anlge"};
    double[] values = new double[] {arr,a, b, c, d, ang};

    chart.setSampleValues(0, values);

    // Sets the look of the Chart.
    chart.setMaxValueLineCount(-1);
    chart.setMultiColorOn(true);
    chart.setLegendOn(true);
    chart.set3DModeOn(true);
    chart.setAutomaticRepaintOn(true);
    chart.setDefaultGridLinesOn(true);
    chart.setBarAlignment(1);
    chart.setAutoLabelSpacingOn(true);
    chart.setBarType(0);
    chart.setBarLabelsOn(true);
    chart.setBarLabels(str);
    chart.setMultiSeriesOn(false);
    chart.autoRepaint();
    chart.forceRepaint();

    // Diplay the Chart
    Frame f = new Frame();
    f.add("Center", chart);
    f.setSize(600,600);
    f.show();
    }
    //This method replaces System.out.println()
    public static void print(String str)
    {
    System.out.println(str);
    }


    // This method writes everything that has a debug next to it, it will write to file.
    static void debug(int dbgLevel, String msg)
    {
    //if(dbgLevel < 2)
    // return;
    if(isStandardOutput)
    System.out.println(msg);
    else
    {
    if(outStream == null)
    {
    try
    {
    outStream = new FileWriter("dump.txt");
    }
    catch(IOException exc)
    {
    System.out.println(" ===== CAN'T OPEN DUMP FILE =====");
    return;
    }
    }
    try
    {
    outStream.write(msg+"\n");
    }
    catch(IOException exc)
    {
    System.out.println(" ===== CAN'T WRITE INTO DUMP FILE =====");
    return;
    }
    }
    }
    }

    As you can see I am printing to a TXT file. I would like to print an XML format instead of a TXT file.

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    Why do you want it in XML? It hardley seems worth it. Don't bother if it's just because 'XML is cool'. That's naff - find a solution which would actually benefit from it.

    (and, by the way, my solution needs no special imports).

    ArchAngel.
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Mar 2003
    Posts
    6
    I would like c ause the plain text comes out to messy, what would you suggest? I try to put in new line so I get a new line in between the text but for some reason it does not work.

  8. #8
    Join Date
    Mar 2003
    Posts
    834
    An extra blank line should be added if you include another '\n' to the FileWriter.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center