所以我和朋友正在研究这个项目,我们一直在试图弄清楚如何从XML文件中的单个文本节点中提取值。他能够提出一些代码来提取我们正在寻找的文本节点,但是有一个小问题。当我运行以下代码时,它工作正常并提取我们需要的东西......
JAVA CODE
import java.io.File;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class Test extends DefaultHandler
{
StringBuffer buffer;
String heading;
boolean inHeading;
public static void main(String[] args)
{
try
{
Test saxNames = new Test();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new File("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items/ProposalOne.docx - Extracted Items/word/document.xml"), saxNames);
}
catch(Exception e)
{
e.printStackTrace(System.err);
}
}
public void startElement(String uri, String localName, String qName, Attributes attrs)
{
if ("w:pStyle".equals(qName))
{
String val = attrs.getValue("w:val");
if (val.contains("Heading"))
{
if (isHeading(val))
{
System.out.println(val);
inHeading = true;
}
}
}
if("w:t".equals(qName))
{
String val = attrs.getValue("w:t");
if (inHeading == true)
{
buffer = new StringBuffer();
}
}
}
public void characters(char buff[], int offset, int length) throws SAXException
{
String s = new String(buff, offset, length);
if(buffer != null)
{
buffer.append(s);
heading = heading += s;
}
}
public void endElement(String uri, String localName, String qName)
{
buffer = null;
if ("w:p".equals(qName) && inHeading == true)
{
System.out.println(heading);
heading = "";
inHeading = false;
}
}
private static boolean isHeading(String heading)
{
String headingNumber = heading.substring(7,8);
String headingName = heading.substring(0,7);
if (headingName.equals("Heading"))
{
if (headingNumber.equals("1")
|| headingNumber.equals("2")
|| headingNumber.equals("3")
|| headingNumber.equals("4")
|| headingNumber.equals("5")
|| headingNumber.equals("6"))
{
return true;
}
}
return false;
}
}
但是,由于这只是我们正在开展的大型项目的一部分,我希望能够将其合并到一个驱动程序类中。因此,我希望能够将main方法转换为构造函数,所以当我改变时......
public static void main(String[] args)
{
try
{
Test saxNames = new Test();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new File("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items/ProposalOne.docx - Extracted Items/word/document.xml"), saxNames);
}
catch(Exception e)
{
e.printStackTrace(System.err);
}
}
以....
public Test()
{
try
{
Test saxNames = new Test();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new File("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items/ProposalOne.docx - Extracted Items/word/document.xml"), saxNames);
}
catch(Exception e)
{
e.printStackTrace(System.err);
}
}
当我尝试从我的驱动程序运行此命令时,我收到以下错误....
Exception in thread "main" java.lang.StackOverflowError
at Test.<init>(Test.java:17)
at Test.<init>(Test.java:17)
at Test.<init>(Test.java:17)
at Test.<init>(Test.java:17)
at Test.<init>(Test.java:17)
.
.
.
at Test.<init>(Test.java:17)
我不知道为什么会这样做。查看代码,我想我可能已经知道错误的来源。在构造函数中,它使用相同的名称实例化另一个构造函数---&gt;因此,为什么它在主要方法中起作用。但是,我不确定如何解决这个问题?
答案 0 :(得分:5)
每次创建Test实例时,都会创建一个新的Test实例。看到错误?
请在您的try语句中执行此操作:
// DON'T DO THIS: Test saxNames = new Test();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new File("blabla_filename.xml"), this);
答案 1 :(得分:3)
在构造函数中,您正在调用构造函数...
公开测试() { 尝试 { 测试saxNames = new Test(); ...