我在一个文件夹中有一些XML文件。我想将XML数据文件写入文本文件。但我不明白该怎么做。 :(
示例XML文件:
<?xml version = "1.0"?>
<note>
<to> Mat </to>
<from> Tim </from>
<head> Black </head>
<body> Yellow </body>
</note>
这是我的代码:
public class ReadXML extends DefaultHandler {
Boolean noteTag = false;
Boolean toTag = false;
Boolean fromTag = false;
Boolean headTag = false;
Boolean bodyTag = false;
static final String NOTE = "note";
static final String TO = "to";
static final String FROM = "from";
static final String HEAD = "head";
static final String BODY = "body";
public void read() throws Exception {
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
File folder = new File("D:\\Source Code\\NetBeans\\Java\\BGPU\\ParseXMLFile");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile() && file.getName().endsWith(".xml")) {
saxParser.parse(file, this);
System.out.println();
}
}
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase(NOTE)) {
noteTag = true;
}
if (qName.equalsIgnoreCase(TO)) {
toTag = true;
}
if (qName.equalsIgnoreCase(FROM)) {
fromTag = true;
}
if (qName.equalsIgnoreCase(HEAD)) {
headTag = true;
}
if (qName.equalsIgnoreCase(BODY)) {
bodyTag = true;
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (fromTag.equals(true)) {
System.out.println("FROM: " + new String(ch, start, length));
}
if (toTag.equals(true)) {
System.out.println("TO: " + new String(ch, start, length));
toTag = false;
}
if (headTag.equals(true)) {
System.out.println("HEAD: " + new String(ch, start, length));
headTag = false;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase(NOTE)) {
noteTag = false;
}
if (qName.equalsIgnoreCase(TO)) {
toTag = false;
}
if (qName.equalsIgnoreCase(FROM)) {
fromTag = false;
}
if (qName.equalsIgnoreCase(HEAD)) {
headTag = false;
}
if (qName.equalsIgnoreCase(BODY)) {
bodyTag = false;
}
}
public void save(String filename) throws Exception {
}
请帮我完成save()方法。
答案 0 :(得分:1)
我建议不使用SAX,这是使用XPath的示例。您可以使用java.util.FileWriter写入文件。
public class Test {
public static void main(String[] args) throws Exception {
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("test.xml");
XPathFactory xPathFactory = XPathFactory.newInstance();
writer.append(xPathFactory.newXPath().compile("//note/to").evaluate(document));
writer.newLine();
writer.append(xPathFactory.newXPath().compile("//note/from").evaluate(document));
writer.newLine();
writer.append(xPathFactory.newXPath().compile("//note/head").evaluate(document));
writer.newLine();
writer.append(xPathFactory.newXPath().compile("//note/body").evaluate(document));
writer.newLine();
writer.close();
}
}
如果你真的必须使用SAX,这是一种更简洁的方法
public class ReadXml {
public static void main(String[] args) throws Exception {
new ReadXml().read();
}
public void read() throws Exception {
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
File folder = new File(".");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile() && file.getName().endsWith(".xml")) {
Handler handler = new Handler();
saxParser.parse(file, handler);
save(handler, file.getName() + ".txt");
}
}
}
private void save(Handler handler, String filename) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.append(handler.getFrom());
writer.newLine();
writer.append(handler.getTo());
writer.newLine();
writer.append(handler.getHead());
writer.newLine();
writer.append(handler.getBody());
writer.newLine();
writer.close();
}
private class Handler extends DefaultHandler {
private StringBuilder content;
private String to;
private String from;
private String body;
private String head;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
content = new StringBuilder();
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException {
content.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if ("to".equals(qName)) {
to = content.toString();
} else if ("from".equals(qName)) {
from = content.toString();
} else if ("body".equals(qName)) {
body = content.toString();
} else if ("head".equals(qName)) {
head = content.toString();
}
}
public String getTo() {
return to;
}
public String getFrom() {
return from;
}
public String getBody() {
return body;
}
public String getHead() {
return head;
}
}
}