有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用EclipseLuna创建Java、XML应用程序

如何:将XML文件插入Java应用程序,然后编译/运行

将XML与Java结合使用的第一天。我正在研究代码/语法,但我不知道如何“添加”这个。将xml转换为我的Java应用程序并运行该应用程序

我正在使用Windows8.1、EclipseLuna和JavaSE8。我尝试了一些在网上找到的不同建议,但没有成功

在我的一次尝试中,这是一条错误消息,我不确定所有尝试中的错误消息是什么,但我认为它是相似的,如果不是相同的话

我还包括了。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>

共 (1) 个答案

  1. # 1 楼答案

    您可能得到了FileNotFoundException,因为您的程序没有在XML所在的文件夹中执行。 只需将Input.xml放在类旁边,并使用Document doc = Builder.parse(getClass().getResourceAsStream("Input.xml"));处理文件。这样你就不需要关心路径了