Creating XML files from Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    Creating XML files from Java

    Hi there!
    I'm trying to create a XML file from within Java. After doing a bit of research, I'm quite confused - there seem to be many, many classes in the standard libraries (plus many more outside), that somehow deal with XML, but I don't know, which ones would be right for me. Here's what I want to do:

    I want to read TXT files (no problem with that), extract the right information (still no problem so far) and then create an XML file, that holds the data I just extracted from the TXT files. I have designed a structure for my XML files, now I need the computer to create them for me.

    So, which classes would you recommend and can you either give me an example of how to use them or direct me to a tutorial with a good example?

    Greetings,
    Nepomuk
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    I'd suggest that you start with JAXP

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by r035198x
      I'd suggest that you start with JAXP
      Thanks for the link, but I'm not sure, it's what I'm looking for. Here's why:
      Originally posted by wikipedia
      The Java API for XML Processing [...] is one of the Java XML programming APIs. It provides the capability of validating and parsing XML documents.
      I'm not trying to validate or parse any XML documents (at the moment), I'm trying to create them. I also checked at saxproject.org and it seems, they only read files instead of creating them in their examples. Also, I had a look through the org.xml.sax library with Eclipse and couldn't find anything about writing.

      Have I just checked the wrong places or do I need something else? If so, what?

      Thanks,
      Nepomuk

      *EDIT* I just found the interface XMLStreamWriter - might that be the right path to be looking at? Or maybe the XMLEncoder? (Although I wasn't planning to use JavaBeans, but it might be a good idea - I'll have to think about it.)

      *EDIT 2* OK, it probably isn't the XMLStreamWriter , as I need something, that's available in JRE 1.4.x, and that doesn't seem to. Still checking out the XMLEncoder.
      Last edited by Nepomuk; Aug 13 '08, 08:28 AM. Reason: Found something

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        OK, I guess I'll probably just use a FileWriter. If anyone has a better suggestion, please come forward with it!

        Greetings,
        Nepomuk

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          First understand how xml is read... and then write it.

          Use a printWriter and write println("<" + name + attribute + ... + ">"), then print(" ") <-- tab, then write the next one, and when you're closing that specific bracket use println("</" + name + attribute + ... + ">").

          Perhaps you could just create methods that automatically did that, like openBracket(nam e, attribute = "", other attribute = "") and closeBracket(na me, attribute = "", other attribute = "")?

          Good luck,
          -blazed

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by blazedaces
            First understand how xml is read... and then write it.
            Thanks, but as I said, I've already created the structure. I've even written a quite long XML file in that format by hand, but now I want the computer to do that for me.
            Originally posted by blazedaces
            Use a printWriter and write...
            As I said, I guess I'll use a FileWriter (OK, a PrintWriter ^^), but thanks for going in to more detail. :-D
            Originally posted by blazedaces
            Perhaps you could just create methods that automatically did that, like openBracket(nam e, attribute = "", other attribute = "") and closeBracket(na me, attribute = "", other attribute = "")?
            Well, it would be more like[code=java]public void startTag(String name, String[] attributes, String[] values);
            // can't think of a more elegant way that's worth the effort to have attributes and their values at the moment
            public void endTag(String name);[/code]or similar, but of course I could do it your way.
            Originally posted by blazedaces
            Good luck,
            -blazed
            Thanks! :-)

            Greetings,
            Nepomuk

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              I once crafted a little extension class to the Writer class that could wrap another
              Writer. It had three additional methods: push(String), pushpop(String) and pop().
              Internally it had a stack of tags, the push() method wrote the opening tag and
              its optional arguments and pushed it on the stack. <tag ...>

              The pushpop() thing did the same but closed the tag immediately <tag .../> and
              that last method (I don't remember whether or not it took a parameter) popped
              a tag from that stack <tag/>

              It worked for the folks I wrote it for ;-) Nowaday I would use a bit of varargs for
              the push() and pushpop() methods.

              kind regards,

              Jos

              Comment

              • rajujrk
                New Member
                • Aug 2008
                • 107

                #8
                I think below code will help you..

                [ spoonfeeding code deleted - moderator ]

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  @rajujrk: read the 'Help' link near the top of this page; we don't hand out boilerplate
                  code here. We try to help people understand instead of spoonfeeding them.
                  Also, if you have to post snippets of code use the [code] ... [/code] tags.

                  kind regards,

                  Jos (moderator)

                  Comment

                  • kokababu
                    New Member
                    • Jul 2008
                    • 39

                    #10
                    If you want to create xml in java you can use DOM api or stAX api.

                    Comment

                    • Nepomuk
                      Recognized Expert Specialist
                      • Aug 2007
                      • 3111

                      #11
                      OK, maybe I should say that I've solved the issue by writing a wrapper to the BufferedWriter class. However, it doesn't use a stack as Jos' one did. As the structure of my file is quite simple, I could easily write few functions to do most of my work. Of course, anyone who wants to suggest I rewrite the code in a much easier way, feel free to do so! :-D

                      Just a suggestion, maybe someone would enjoy writing an article about XML and Java? How to read and write XML files, JAXB, JAXP and whatever they're all called. I'm sure, that would help people a great deal ever so often. I'd do it myself if a) I wasn't so busy, b) I hadn't already started 2 articles that I still have to complete and c) I knew enough about it.

                      Greetings,
                      Nepomuk

                      Comment

                      Working...