簡體   English   中英

使用Eclipse Luna創建Java,XML應用程序

[英]Creating a Java, XML application with Eclipse Luna

如何:將XML文件插入Java應用程序,然后進行編譯/運行?

使用Java與XML的第一天。 我正在通過代碼/語法確定,但是我無法確定如何將該.xml“添加”到我的Java應用程序中並運行該應用程序。

我使用的是Window 8.1,eclipse Luna,Java SE8。我嘗試了一些在網上找到的不同建議,但是沒有運氣。

在我的嘗試之一中,這是錯誤消息,我不確定所有嘗試的錯誤消息都是什么,但是我認為它是相似的,即使不相同。

我還包括了.java和.xml。 據我所知,我的問題可能在任何地方,所以我願意接受所有建議。 但是您可能必須從頭開始逐步實現這一目標。

感謝您的考慮和努力。

 java.io.FileNotFoundException: C:\\Users\\Reed\\workspace\\JavaXMLDOMParser\\input.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source) at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) at javaXMLDOMParse.DomParserPuCm.main(DomParserPuCm.java:23) 

 package javaXMLDOMParse; import java.io.File; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; public class DomParserPuCm { public static void main(String[] args) { try { File inputFile = new File("Input.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("student"); System.out.println("----------------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Student roll no : " + eElement.getAttribute("rollno")); System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent()); System.out.println("Marks : " + eElement.getElementsByTagName("marks").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } 
 <?xml version="1.0"?> <class> <student rollno="393"> <firstname>dinkar</firstname> <lastname>kad</lastname> <nickname>dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>jasvir</firstname> <lastname>singn</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> </class> 

您可能會收到FileNotFoundException因為您的程序未在XML所在的文件夾中執行。 只需將您的Input.xml放在您的類旁邊,然后使用Document doc = Builder.parse(getClass().getResourceAsStream("Input.xml")); 處理文件。 這樣,您無需關心路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM