简体   繁体   中英

writing string to file in java

Below two classes. I have a problem with writing string to file in java. Why in my file xml.txt I get null? Why I can't write String a = px.readXml(url) ? In xml.txt I've only 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 i = 0; i<children.getLength();i++)
        {
            Node child = children.item(i);
            if (child instanceof Element)
            {
                Element childElement = (Element)child;
                NodeList childrenPozycja = childElement.getChildNodes();
                for (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 is 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 a = px.readXml(url);        
            px.writeXml(a);
        }
    }
}

To return value of listaKursow you can add return statement in readXml() like below:

    public String readXml(URL url) throws ParserConfigurationException, MalformedURLException, IOException, SAXException
    {
      //...
      //Declare listaKursow here
      String listaKursow = "";
      for(int i = 0; i<children.getLength();i++)
      {
        //..
           for (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 you should note that this would return first value of listaKursow in Xml.
If you are looking for all values of elements as String, you can add them to a List and return list.toString() or concatenate String variable in each iteration.

EDIT: Based on your inputs I have modified my post to return all lists

@hitz answer is good, but I would suggest using StringBuilder for this. It is far more efficient and nicer to look at.

public String readXml(URL url) throws ParserConfigurationException, MalformedURLException, IOException, SAXException
{
    //...
    //Declare listaKursow here
    final StringBuilder listaKursow = new StringBuilder();
    for(int i = 0; i<children.getLength();i++)
    {
        //..
        for (int j = 0; j<childrenPozycja.getLength(); j++)
        {
            final Node childPozycja = childrenPozycja.item(j);
            if (childPozycja instanceof Element)
            {
                final String nameChf = "CHF";
                Double kurs;
                final Element childPozycjaElement = (Element) childPozycja;
                listaKursow.append( childPozycjaElement.getTextContent() );
            }
        }
  }
  return listaKursow.toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM