gpt4 book ai didi

java - 从网络读取 xml

转载 作者:行者123 更新时间:2023-12-02 11:12:41 24 4
gpt4 key购买 nike

我正在从特定的 url 读取 xml。但我收到此错误

[Fatal Error] :3:24: Open quote is expected for attribute "http-equiv" associated with an element type "META".

xml 缺少编码 UTF-8,我添加了它,但仍然收到此错误。我们将非常感谢您的帮助。

这是我的代码:

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class crawleycraw {

public static void main(String[] args) throws IOException, TransformerException, SAXException, ParserConfigurationException {
// TODO Auto-generated method stub
String urlString = "http://www.bnb.bg/";
URL url = new URL(urlString);
URLConnection conn = url.openConnection();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());

TransformerFactory factoryl = TransformerFactory.newInstance();
Transformer xform = factoryl.newTransformer();

Transformer transformer = null;
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"no");

xform.transform(new DOMSource(doc), new StreamResult(System.out));
}

}

最佳答案

URL http://www.bnb.bg/ 将返回 HTML,而不是 XML。因此,当您解析它时,它将抛出错误,因为它是 HTML,而不是 XML。

您的代码还有其他问题,Srinevu 已经回答了

Even if you download above URL using curl or wget or browser, save it as example.xml, use any XML editor, you will see exact same error, you are seeing by Java parser.

String urlString = "http://www.bnb.bg/";

关于java - 从网络读取 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50510249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com