Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Problem parsing HTML file into Document

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: "Nick Day" <the_doctor_nick AT hotmail.com>
  • To: wolfgang.hoschek AT mac.com
  • Cc: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] Problem parsing HTML file into Document
  • Date: Mon, 12 Dec 2005 03:15:42 +0000

Hi,

Many thanks for the advice!

I am now using TagSoup to parse HTML documents, however I have run into a problem when attempting to query the resulting Documents using XPath. I have set up a test method (bottom) which reads in a HTML page from a specified URL and then queries it using XPath. The matching elements are then printed out.

If, for example I use http://wwmm.ch.cam.ac.uk/data/kegg/cml_inchi/C12384.cml and query it using //* I get the following printout:

NUMBER OF MATCHING ELEMENTS: 100
Element number: 0 [nu.xom.Element: html]
Element number: 1 [nu.xom.Element: body]
.... more element declarations
Element number: 99 [nu.xom.Element: bond]
FINISH

But if I try to match against any element in particular (e.g. using //body) then the result I get is:

NUMBER OF MATCHING ELEMENTS: 0
FINISH

So the individual elements are being recognised, but I can't seem to retrieve them using the query method. Does anyone else have any experience with this?

many thanks,
Nick

public static void main(String[] args) {
Document doc = null;
try {
XMLReader tagsoup = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");
Builder builder = new Builder(tagsoup);
doc = builder.build("http://wwmm.ch.cam.ac.uk/data/kegg/cml_inchi/C12384.cml";);
} catch (SAXException e) {
e.printStackTrace();
} catch (ValidityException e) {
e.printStackTrace();
} catch (ParsingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

String xpath = "//*";
Nodes elements = doc.query(xpath);
System.out.println("NUMBER OF MATCHING ELEMENTS: " +
elements.size());
for (int i = 0; i < elements.size(); i++) {
System.out.println("Element number: " + i + " " + elements.get(i).toString());
}
System.out.println("FINISH");
}

-------------------------------------------------------------------------------------------------------------------------------------------






From: Wolfgang Hoschek <wolfgang.hoschek AT mac.com>
To: Nick Day <the_doctor_nick AT hotmail.com>
CC: xom-interest AT lists.ibiblio.org
Subject: Re: [XOM-interest] Problem parsing HTML file into Document
Date: Sat, 10 Dec 2005 17:55:00 -0800

HTML is not wellformed XML, so you need to preprocess it with something like TagSoup.
Tagsoup makes HTML appear as if it were XML. See http:// mercury.ccil.org/~cowan/XML/tagsoup/

Wolfgang.

On Dec 10, 2005, at 3:09 PM, Nick Day wrote:

Hi,

I've been having a bit of trouble parsing a HTML file (attached) into a document and keep getting the error below. Do I need to preprocess the file into a different encoding before attempting the creation of the Document object or am I way off?

thanks,
Nick

nu.xom.ParsingException: Invalid byte 1 of 1-byte UTF-8 sequence. in file:/f:/test.txt
at nu.xom.Builder.build(Builder.java:1157)
at nu.xom.Builder.build(Builder.java:614)
at nu.xom.Builder.build(Builder.java:990)
at uk.ac.cam.ch.wwmm.scuflworkers.java.StripElementsFromXMLTest.main (StripElementsFromXMLTest.java:23)
Caused by: java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence.
at org.apache.xerces.impl.io.UTF8Reader.invalidByte(Unknown Source)
at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.scanLiteral(Unknown Source)
at org.apache.xerces.impl.XMLScanner.scanAttributeValue(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanAttribute (Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement (Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl $FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument (Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at nu.xom.Builder.build(Builder.java:1127)
... 3 more
Exception in thread "main" java.lang.NullPointerException
at uk.ac.cam.ch.wwmm.scuflworkers.java.StripElementsFromXml.strip (StripElementsFromXml.java:22)
at uk.ac.cam.ch.wwmm.scuflworkers.java.StripElementsFromXMLTest.main (StripElementsFromXMLTest.java:34)


<test.txt>
_______________________________________________
XOM-interest mailing list
XOM-interest AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest








Archive powered by MHonArc 2.6.24.

Top of Page