在java中将字符串写入文件

时间:2015-05-29 19:47:06

标签: java string file null

以下两个班级。我在java中将字符串写入文件时遇到问题。为什么在我的文件xml.txt中我得到null?为什么我不能写String a = px.readXml(url)?在xml.txt中我只有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;
    }
}

这里是测试类:

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);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

要返回listaKursow的值,您可以在return中添加readXml()语句,如下所示:

    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;
    }

但是你应该注意,这将在Xml中返回listaKursow的第一个值 如果要将所有元素值作为String查找,可以将它们添加到List并在每次迭代中返回list.toString()或连接String变量。

编辑:根据您的输入,我修改了帖子以返回所有列表

答案 1 :(得分:0)

@hitz答案很好,但我建议使用StringBuilder。它看起来效率更高,效果更好。

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();
}