so a friend and I are working on this project and we'd been trying to figure out how to pull out the values from individual text nodes within an XML file. He was able to come up with a bit of code that extracts the text nodes we are looking for, but there is one slight problem. When I run the following code, it works fine and extracts what we need...
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;
}
}
However, since this is only a portion of the larger project we are working on, I want to be able to incorporate this into a driver class. Thus I want to be able to turn the main method here into a constructor, and so when I change...
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);
}
}
to....
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);
}
}
And when I try to run this from my driver, I get the following error....
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)
I have no clue why it is doing this. Looking over the code, I think I may have an idea of where the error is coming from. Within the constructor, it instantiates another constructor by the same name ---> Hence why it 开发者_如何学运维works in the main method. However, I'm not really sure how to address that issue?
You create a new instance of Test, each time you create an instance of Test. See the error?
Do this instead in your try-statement:
// DON'T DO THIS: Test saxNames = new Test();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new File("blabla_filename.xml"), this);
In your constructor you are calling your constructor ...
public Test() { try { Test saxNames = new Test(); ...
精彩评论