9 Replies Latest reply on Jun 28, 2011 9:01 PM by darrylburke

    javaFX and  Create xml file

      Hi to all , i want create a xml file, in java i used jdom in javafx is similar? in search example but i see only the read a xml file, can you see me a simple example in javafx?
      thx for yours help
      best regads
        • 1. Re: javaFX and  Create xml file
          You can use any xml parser(dom, jdom,StAX). I prefer StAX (Streaming API for XML) . It is a part of JDK 6.
          • 2. Re: javaFX and  Create xml file
            thanks for your answer, can you know example or how-to for use in javafx?
            • 3. Re: javaFX and  Create xml file
              An example of how to build an XML document is here: http://www.genedavis.com/library/xml/java_dom_xml_creation.jsp

              The javadoc for DOM is here: http://download.oracle.com/javase/7/docs/api/org/w3c/dom/package-summary.html

              You can probably figure out the rest. Unless you are using the deprecated scripting language, there is nothing special to worry about when using it with JavaFX (though for performance reasons, you might want to run the XML creation in a separate thread from JavaFX's own threads).

              Edited by: aidreamer on Jun 25, 2011 4:24 PM
              • 4. Re: javaFX and  Create xml file
                thanks for your answer, i know use java and xml, my problem i must use JAVAFX because this is a project for my thesis for end the university :(
                now i hope to convert the example from java to javafx
                • 5. Re: javaFX and  Create xml file
                  I still don't really understand what you are asking. Could you please clarify?

                  If you already know how to do XML in Java, then doing it with JavaFX 2.0 is no different since it is just another Java API.

                  Are you asking how to do it in the deprecated JavaFX Scripting language? The only difference there is syntax. If you have already learned how to code in JavaFX Script, there still shouldn't be a problem. You would use the same API as you would in Java, because as far as I can tell, the only JavaFX Script specific XML API is for reading, not writing.

                  Edited by: aidreamer on Jun 27, 2011 7:58 PM
                  • 6. Re: javaFX and  Create xml file
                    i want to create a file xml, i read this example in java ( http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/ similar to you wrote me )
                    i write this:
                                    var doc:Document;
                                         var elem:Element;                    
                              var transformer:Transformer ;
                             var transformerFactory : TransformerFactory;
                                 var source:DOMSource;  
                                var result: StreamResult;
                         result.setOutputStream(var file: File{"test.xml"});        // i have a error 
                               transformer.transform(source, result);
                    but don't work because in javaFX i don't know to write a file :(


                    Edited by: De Curtis on 28-giu-2011 1.48

                    Edited by: De Curtis on 28-giu-2011 1.49
                    • 7. Re: javaFX and  Create xml file
                      You create references to Java objects the way you would JavaFX objects. But instantiating them works the same way as it does in Java:
                      var file : File = new File("SomeFile.txt");
                      doSomeThingWithAFile(new File("AnotherFile.txt"));
                      • 8. Re: javaFX and  Create xml file
                        now work well thx
                        now write this code for other people :)
                        var docFactory :DocumentBuilderFactory  = DocumentBuilderFactory.newInstance();
                        var docBuilder :DocumentBuilder  = docFactory.newDocumentBuilder();
                         //root elements
                        var doc     : Document  = docBuilder.newDocument();
                        var rootElement: Element  = doc.createElement("company");
                        //staff elements
                        var staff : Element  = doc.createElement("Staff");
                          //firstname elements
                             var firstname:  Element  = doc.createElement("firstname");
                          //lastname elements
                             var lastname : Element = doc.createElement("lastname");
                               lastname.appendChild(doc.createTextNode("mook kim"));
                         //nickname elements
                             var nickname : Element  = doc.createElement("nickname");
                               //salary elements
                        var salary : Element = doc.createElement("salary");
                                 //write the content into xml file
                             var transformerFactory:  TransformerFactory  = TransformerFactory.newInstance();
                             var transformer:  Transformer  = transformerFactory.newTransformer();
                             var source:  DOMSource  = new DOMSource(doc);
                             var result:  StreamResult  =  new StreamResult(new File("C:\\33\\testing.xml"));
                               transformer.transform(source, result);
                        • 9. Re: javaFX and  Create xml file
                          De Curtis wrote:
                          now write this code for other people :)
                          Thank you for sharing.