我使用以下代码读取给定 URL 的 XML 数据,但出现错误,我不知道如何解决。我想阅读给定 URL 的内容,但不是在 HTML 中的 XML 上。任何帮助将不胜感激。
编码 :
package search;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class Process{
public static void main ( String [] args ) throws IOException{
URL xmlUrl = new URL("http://www.yahoo.com");
InputStream in = xmlUrl.openStream();
Document doc = parse(in);
}
public static Document parse (InputStream is) {
Document ret = null;
DocumentBuilderFactory domFactory;
DocumentBuilder builder;
try {
domFactory = DocumentBuilderFactory.newInstance();
domFactory.setValidating(false);
domFactory.setNamespaceAware(false);
builder = domFactory.newDocumentBuilder();
ret = builder.parse(is);
}
catch (Exception ex) {
System.err.println("unable to load XML: " + ex);
}
return ret;
}
}
错误
[Fatal Error] :7:17: The entity "lrm" was referenced, but not declared.
unable to load XML: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 17;
The entity "lrm" was referenced, but not declared.