Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java code to embedding xml tags at start and end of file

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Java code to embedding xml tags at start and end of file

    Good afternoon all!

    I've got a question again involving horrible xml!

    I'm just curious does anyone know how i could write some code that places an open xml tag at the start of a file and a closed xml tag at the end of file whenever i write the output of a program to a file so that the output is between both tags?

    Thanks

    John


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: embedding xml tags at start and end of file

    Hello John,

    How about this? First, add a text file in the same directory as this application called textfile1.txt

    This code will read in the file, and write it out again as textfile1_new.txt with the XML tags at the start and end of the file.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.FileInputStream;
    import java.io.*;
     
    public class XMLTagger {
     
        public static String xmlOpen = "<xmltag>";
        public static String xmlClose = "</xmltag>";
        public static Writer output = null;
     
        public void OpenWriteFile() {
     
            String newLine = System.getProperty("line.separator");
     
            try {
                FileInputStream in = new FileInputStream("textfile1.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
     
                File file = new File("textfile1_new.txt");
                output = new BufferedWriter(new FileWriter(file));
                output.write(xmlOpen);
                output.write(newLine);
                String strLine;
     
                while ((strLine = br.readLine()) != null) {
                    // System.out.println(strLine);
                    output.write(strLine);
                    output.write(newLine);
                }
     
                output.write(xmlClose);
                output.write(newLine);
                output.close();
     
                System.out.println("New File Written");
     
            } catch (Exception e) {
                System.out.println("Ouch! " + e);
            }
     
        }
     
        public static void main(String[] args) throws Exception {
     
            XMLTagger xml = new XMLTagger();
            xml.OpenWriteFile();
        }
     
    }
    Example output:

    <xmltag>
    this is textfile1, an example text file
    this is textfile1, an example text file
    this is textfile1, an example text file
    this is textfile1, an example text file
    this is textfile1, an example text file
    this is textfile1, an example text file
    </xmltag>
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    John (April 30th, 2009)

  4. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: embedding xml tags at start and end of file

    Hey thanks for the reply JavaPF

    The solution worked great! Only had a chance to get on and have a look at it today so sorry for the late reply.

    Basically in my program i just added the following before my output:
    output.write(xmlClose);
    output.write(newLine);

    and then he following after my output:
    output.write(xmlClose);
    output.write(newLine);

    with also one or two tweaks with the code you provided.

    I'm nearly there with my program!
    Just need to implement the DOMParse in my program to parse out the xml once the whitespace has been removed!

    Will keep you updated with progress
    Don't think it's going to do exactly what i originally intended but not far off

    Thanks again

    john

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: embedding xml tags at start and end of file

    Glad I could help John
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. How should i write and compile java with Ubuntu?
    By Talk Binary in forum Java IDEs
    Replies: 19
    Last Post: May 7th, 2009, 05:29 AM
  2. Replies: 1
    Last Post: May 7th, 2009, 04:31 AM
  3. [SOLVED] Parsing ID3 tags from mp3
    By John in forum Java Theory & Questions
    Replies: 14
    Last Post: April 16th, 2009, 01:36 PM
  4. Best way to learn java for beginners
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: May 8th, 2008, 04:37 AM

Tags for this Thread