当前位置: 代码迷 >> J2SE >> 急 关于java对xml的读取解决方法
  详细解决方案

急 关于java对xml的读取解决方法

热度:10   发布时间:2016-04-24 14:53:17.0
急 关于java对xml的读取
我的java考试系统总是读取试题为空     试题文件采用xml文件
大家帮忙看看   哪里出毛病了   谢谢.
下面是代码


import   javax.xml.parsers.*;
import   org.w3c.dom.*;
import   java.io.*;
import   java.util.*;
import   java.lang.String;
public   class   ReadTestquestion
{       static   int   z;
static   String   filename   ,
correctAnswer   =   " ",
testContent   =   " ",
selection   =   " ",
selection1   =   " ";
int   score   =   0;
long   time   =   0;
boolean   完成考试   =   false;
FileReader   in   =   null;
String   te;
Node   currentNode,   textNode,   currentNode1;
NodeList   childNodes,   childNodes1,   childNodes2;
String   temp;
String   a[]   =   new   String[100];
StringBuffer   temp1,temp2;
public   void   setFilename(String   name)
{
filename   =   name;
score   =   0;
selection   =   " ";
try
{
if   (in   !=   null   &&   temp   !=   null)
{
in.close();
}
DocumentBuilderFactory   factory   = DocumentBuilderFactory.newInstance();
DocumentBuilder   builder   =   factory.newDocumentBuilder();
Document   document   =   builder.parse(new   File(filename));
Node   root   =   document.getDocumentElement();

childNodes   =   root.getChildNodes();
currentNode   =   childNodes.item(1);
String   s   =   currentNode.getNodeName();
String   s1   =   currentNode.getNodeValue();
childNodes1   =   currentNode.getChildNodes();
textNode   =   childNodes1.item(0);
String   q   =   textNode.getNodeName();
String   temp   =   textNode.getNodeValue();
StringTokenizer   token   =   new   StringTokenizer(temp,   ": ");
int   hour   =   Integer.parseInt(token.nextToken());
int   minute   =   Integer.parseInt(token.nextToken());
int   second   =   Integer.parseInt(token.nextToken());
time   =   1000   *   (second   +   minute   *   60   +   hour   *   60   *   60);
}
catch   (Exception   e)
{
System.out.print( "Err   happend   here.   "   +   e.getMessage());
testContent   =   "请选择试题文件! ";
}
}
public   String   getFilename()
{
return   filename;
}
public   long   getTime()
{
return   time;
}
public   void   set完成考试(boolean   b)
{
完成考试   =   b;
}
public   boolean   get完成考试()
{
return   完成考试;
}

public   String   getTestContent()
{   try
{
DocumentBuilderFactory   factory   =
DocumentBuilderFactory.newInstance();
DocumentBuilder   builder   =   factory.newDocumentBuilder();
Document   document   =   builder.parse(new   File(filename));
Node   root   =   document.getDocumentElement();

childNodes   =   root.getChildNodes();
  相关解决方案