gpt4 book ai didi

java - XML 解析错误

转载 作者:行者123 更新时间:2023-11-29 03:55:27 25 4
gpt4 key购买 nike

为了解析一个 XML 字符串,我用谷歌搜索并找到了 an example on how to do that ,但这对我不起作用!

当我声明 NodeList 时出现以下错误:required:groovy.util.NodeList, found org.w3c.dom.NodeList

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
System.out.println("*********test2*********");
InputSource is = new InputSource(new StringReader(var2));
Document parse = builder.parse(is);
NodeList nodes = parse.getElementsByTagName("step");

for (int i = 0; i < nodes.getLength(); i++)
{
Element element = (Element) nodes.item(i);

NodeList name = element.getElementsByTagName("step");
Element line = (Element) name.item(0);
System.out.println("Name: " + getCharacterDataFromElement(line));
System.out.println("______test3_____");
}

更新: 我已经使用此代码更改了代码,但出现了另一个错误,我认为它与我的 xml 字符串的结构有关:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(var2));
Document document = documentBuilder.parse(is);

NodeList nl = document.getElementsByTagName("step");
Element el = (Element) nl.item(0);
Text elText = (Text) el.getChunks();
String theValue = elText.getNodeValue();
System.out.println("value"+theValue);

错误:

java.net.UnknownHostException: www.opensymphony.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:677)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1315)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:1282)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:283)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1194)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1090)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1003)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at DAOKPI.Main.main(Main.java:99)

最佳答案

您导入了错误的类:您似乎导入了 groovy.util.NodeList 但需要 org.w3c.dom.NodeList

将源文件顶部的 import groovy.util.NodeList; 替换为 import org.w3c.dom.NodeList;

关于java - XML 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6664051/

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