将字符串写入 java 中的文件
writing string to file in java
低于两个 class。我在 java 中将字符串写入文件时遇到问题。为什么在我的文件 xml.txt 中我得到空值?为什么我不能写 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;
}
}
这里正在测试 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);
}
}
}
对于 listaKursow
的 return 值,您可以在 readXml()
中添加 return
语句,如下所示:
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;
}
但是您应该注意,这会 return Xml 中 listaKursow
的第一个值。
如果您正在寻找所有元素的值作为字符串,您可以将它们添加到 List
和 return list.toString()
或在每次迭代中连接 String
变量。
编辑: 根据您的输入,我已将我的 post 修改为 return 所有列表
@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();
}
低于两个 class。我在 java 中将字符串写入文件时遇到问题。为什么在我的文件 xml.txt 中我得到空值?为什么我不能写 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;
}
}
这里正在测试 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);
}
}
}
对于 listaKursow
的 return 值,您可以在 readXml()
中添加 return
语句,如下所示:
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;
}
但是您应该注意,这会 return Xml 中 listaKursow
的第一个值。
如果您正在寻找所有元素的值作为字符串,您可以将它们添加到 List
和 return list.toString()
或在每次迭代中连接 String
变量。
编辑: 根据您的输入,我已将我的 post 修改为 return 所有列表
@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();
}