我正在尝试将XML文件解析为java类。但它给我一个错误说
无法找到或加载主类
我正在解析的XML文件与PretechDOMReader.java
文件
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class PretechDOMReader {
public static void main(String[] args) {
try {
File fXmlFile = new File("PretechDOM.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Order Details"
+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Ordernumber");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
Node orderedItem1 = (Node) (NodeList) eElement
.getElementsByTagName("OrderedItem1").item(0)
.getChildNodes().item(0);
System.out.println("OrderedItem1 :"
+ orderedItem1.getNodeValue());
Node orderitemPrice = (Node) (NodeList) eElement
.getElementsByTagName("OrderitemPrice").item(0)
.getChildNodes().item(0);
System.out.println("OrderitemPrice :"
+ orderitemPrice.getNodeValue());
Node orderitemTax = (Node) (NodeList) eElement
.getElementsByTagName("OrderitemTax").item(0)
.getChildNodes().item(0);
System.out.println("OrderitemTax :"
+ orderitemTax.getNodeValue());
Node orderDiscount = (Node) (NodeList) eElement
.getElementsByTagName("OrderDiscount").item(0)
.getChildNodes().item(0);
System.out.println("OrderDiscount :"
+ orderDiscount.getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的XML文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Order>
<Ordernumber Number="9988">
<OrderedItem1>Pepsi</OrderedItem1>
<OrderitemPrice>10</OrderitemPrice>
<OrderitemTax>10</OrderitemTax>
<OrderDiscount>20</OrderDiscount>
</Ordernumber>
<Ordernumber Number="9999">
<OrderedItem1>Cocacola</OrderedItem1>
<OrderitemPrice>11</OrderitemPrice>
<OrderitemTax>11</OrderitemTax>
<OrderDiscount>22</OrderDiscount>
</Ordernumber>
答案 0 :(得分:0)
您的代码运行良好。 请在Eclipse中检查项目的Class Path设置。