writing string to file in java -


below 2 classes. have problem writing string file in java. why in file xml.txt null? why can't write string = px.readxml(url) ? in xml.txt i've null

package xml;  import java.io.filewriter; import java.io.ioexception; import java.io.printwriter; import java.net.*; import java.util.stringtokenizer; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.saxexception;  public class printxml {      public string readxml(url url) throws parserconfigurationexception, malformedurlexception, ioexception, saxexception     {         //url url = new url("http://www.nbp.pl/kursy/xml/a093z150515.xml");         documentbuilderfactory factory = documentbuilderfactory.newinstance();         documentbuilder builder = factory.newdocumentbuilder();         document doc = builder.parse(url.openstream());         element root = doc.getdocumentelement();         //system.out.println(root.gettagname());         nodelist children = root.getchildnodes();         int liczbalinijek = children.getlength();         for(int = 0; i<children.getlength();i++)         {             node child = children.item(i);             if (child instanceof element)             {                 element childelement = (element)child;                 nodelist childrenpozycja = childelement.getchildnodes();                 (int j = 0; j<childrenpozycja.getlength(); j++)                 {                     node childpozycja = childrenpozycja.item(j);                     if (childpozycja instanceof element)                     {                         string namechf = "chf";                         double kurs;                         element childpozycjaelement = (element) childpozycja;                         string listakursow = childpozycjaelement.gettextcontent();                          //system.out.println(listakursow);                     }                 }             }         }         return null;     }      public string writexml(string tofile) throws ioexception     {            printwriter out = new printwriter(new filewriter("xml.txt"));         out.println(tofile);         out.close();         return null;     } } 

and here testing class:

package xml;  import java.io.ioexception; import java.io.printwriter; import java.net.*; import java.util.stringtokenizer; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.saxexception;  public class printxmltester {      public static void main(string[] args) throws malformedurlexception,         parserconfigurationexception, saxexception, ioexception {              url url = new url("http://www.nbp.pl/kursy/xml/a093z150515.xml");             printxml px = new printxml();             string = px.readxml(url);                     px.writexml(a);         }     } } 

to return value of listakursow can add return statement in readxml() below:

    public string readxml(url url) throws parserconfigurationexception, malformedurlexception, ioexception, saxexception     {       //...       //declare listakursow here       string listakursow = "";       for(int = 0; i<children.getlength();i++)       {         //..            (int j = 0; j<childrenpozycja.getlength(); j++)             {                 node childpozycja = childrenpozycja.item(j);                 if (childpozycja instanceof element)                 {                     string namechf = "chf";                     double kurs;                     element childpozycjaelement = (element) childpozycja;                     string listakursowtemp = childpozycjaelement.gettextcontent();                      //system.out.println(listakursow);                      //return value of listakursow                     listakursow = listakursow + listakursowtemp;                 }             }       }       return listakursow;     } 

however should note return first value of listakursow in xml.
if looking values of elements string, can add them list , return list.tostring() or concatenate string variable in each iteration.

edit: based on inputs have modified post return lists


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -