下面两个类。我有一个问题,写字符串在java文件。为什么我的xml。txt文件为空?为什么我不能写字符串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
的值,可以在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;
}
但是您应该注意,这将返回Xml中listaKursow
的第一个值。
如果您要查找所有元素的值作为String,您可以将它们添加到List
中,并在每次迭代中返回list.toString()
或连接String
变量。
EDIT:根据您的输入,我修改了我的帖子以返回所有列表
@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();
}