我需要從文件夾中讀取所有輸入xml文件並寫入一個輸出文本文件,逗號分隔。我有下面的代碼程序讀取一個文件,有人可以幫我修改它,並從c:/ java /文件夾中讀取所有文件。感謝從文件夾讀取多個xml文件並寫入一個輸出文本文件(逗號分隔)
import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class XPathExample {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory domFactory = DocumentBuilderFactory
.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("c:/java/test.xml");
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//FORM[@name='F00001'] /*/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
}
你有沒有嘗試過自己的東西? –