我发布的代码全部正常,并且用于编写,但我需要关于如何读取xml文件以便我可以输出它和/或删除文件的建议。我阅读了关于SAX,documentbuilder,parse方法和其他一些方法,但我对使用什么感到困惑。我不需要你为此编写代码,而是指向正确的方向。在一个文件夹中读取muiltiple xml文件
该文件的文件夹中创建单独所以我需要在一次可能的话阅读它们(文件名是下面为标识列出的变量)
这是我如何创建XML文件。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
{
try {
DocumentBuilder doc = factory.newDocumentBuilder();
Document doc1=doc.newDocument();
Element IdNumber = (Element) doc1.createElement(Id);
Element IDes=(Element) doc1.createElement("InitialDestination");
Element FinDes=(Element) doc1.createElement("FinalDestination");
Element HourTime=(Element) doc1.createElement("Hours");
Element Minutetime=(Element) doc1.createElement("Minutes");
Element Price=(Element) doc1.createElement("TicketPrice");
Element Tran=(Element) doc1.createElement("TransportAgency");
doc1.appendChild(IdNumber);
IDes.appendChild(doc1.createTextNode(InDes));
FinDes.appendChild(doc1.createTextNode(FDes));
HourTime.appendChild(doc1.createTextNode(Htime));
Minutetime.appendChild(doc1.createTextNode(Mtime));
Price.appendChild(doc1.createTextNode(TicketPrice));
Tran.appendChild(doc1.createTextNode(TransportAgency));
IdNumber.appendChild(IDes);
IdNumber.appendChild(FinDes);
IdNumber.appendChild(HourTime);
IdNumber.appendChild(Minutetime);
IdNumber.appendChild(Price);
IdNumber.appendChild(Tran);
Source S=new DOMSource(doc1);
File file1=new File("C:\\Users\\Lozanovski\\Desktop\\TransportMe");
file1.mkdirs();
File file=new File("C:\\Users\\Lozanovski\\Desktop\\TransportMe\\"+Id+".xml");
StreamResult R=new StreamResult(file);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(S, R);
}
catch(ParserConfigurationException except)
{
System.out.println(except);
}
catch(TransformerException except1)
{
System.out.println(except1);
}
catch(DOMException except2)
{
System.out.println(except2);
}
catch(NullPointerException except3){
System.out.println(except3);
}
}
我不知道如何正确地张贴XML代码在文件夹(我将接受编辑)
<?xml version="1.0" encoding="UTF-8"?>
-<InsertIdentificationNumber>
<InitialDestination>Insert Initial Destination</InitialDestination>
<FinalDestination>Insert Final Destination</FinalDestination>
<Hours>Insert Hours</Hours>
<Minutes>Insert Minutes</Minutes>
<TicketPrice>Insert Ticket Price</TicketPrice>
<TransportAgency>Insert Transport Agency</TransportAgency>
</InsertIdentificationNumber>
您想将XML文件读入DOM吗? – Joachim 2014-12-13 15:47:23
是的,我想读入DOM,所以我可以使用文件的名称来删除它或输出文件中的所有内容在一个新的帧 – 2014-12-13 15:53:15
比你已经答案,请参阅@Khalids post – Joachim 2014-12-13 15:54:41