9 Replies Latest reply: Jun 28, 2011 3:55 PM by 854171 RSS

    javaFX and  Create xml file

    854171
      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
      A.
        • 1. Re: javaFX and  Create xml file
          shakir.gusaroff
          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
            854171
            thanks for your answer, can you know example or how-to for use in javafx?
            thx
            • 3. Re: javaFX and  Create xml file
              aidreamer
              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
                854171
                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
                A.
                • 5. Re: javaFX and  Create xml file
                  aidreamer
                  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
                    854171
                    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;                    
                                         elem=doc.createElement("root");
                                         doc.appendChild(elem);
                                  
                             
                              var transformer:Transformer ;
                             var transformerFactory : TransformerFactory;
                           transformerFactory=TransformerFactory.newInstance();
                             transformer=transformerFactory.newTransformer();
                                 var source:DOMSource;  
                                source.setNode(doc);
                                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 :(

                    thx
                    A.

                    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
                      aidreamer
                      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
                        854171
                        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");
                               doc.appendChild(rootElement);
                        //staff elements
                        var staff : Element  = doc.createElement("Staff");
                               rootElement.appendChild(staff);
                        
                          //firstname elements
                             var firstname:  Element  = doc.createElement("firstname");
                               firstname.appendChild(doc.createTextNode("yong"));
                               staff.appendChild(firstname);
                        
                          //lastname elements
                             var lastname : Element = doc.createElement("lastname");
                               lastname.appendChild(doc.createTextNode("mook kim"));
                               staff.appendChild(lastname);
                        
                         //nickname elements
                             var nickname : Element  = doc.createElement("nickname");
                               nickname.appendChild(doc.createTextNode("mkyong"));
                               staff.appendChild(nickname);
                        
                               //salary elements
                        var salary : Element = doc.createElement("salary");
                               salary.appendChild(doc.createTextNode("100000"));
                               staff.appendChild(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
                          Darryl Burke
                          De Curtis wrote:
                          now write this code for other people :)
                          Thank you for sharing.

                          db