以下两节课。我在用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;
}
}
这是测试类:
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()
下面添加语句:
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;
}
但是,您应该注意,这将返回listaKursow
Xml中的第一个值。
如果您正在寻找元素的所有值作为String,则可以将它们添加到中,List
并在每次迭代中返回list.toString()
或连接String
变量。
编辑:根据您的输入,我已经修改了我的帖子以返回所有列表
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句