2011-07-12 17 views
1

私はグーグルで行ったXMLストリングを解析するためにan example on how to do thatを見つけましたが、それは私のためには機能しませんでした。XMLの解析でエラーが発生しました

私は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_____"); 
} 

Upadte: 私はこのコードを使用してコードを変更しましたが、別のエラーが、私は私の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) 

答えて

4

間違ったクラスをインポートしました:groovy.util.NodeListをインポートしたようですが、代わりにorg.w3c.dom.NodeListが必要です。

ソースファイルの先頭にある​​をimport org.w3c.dom.NodeList;に置き換えてください。

+0

このエラーが発生しました:symbol:methode getElementsByTagName(java.lang.String)を見つけることができません! – rym

+0

私の投稿を別のコードで更新しましたが、常にエラーがあります:( – rym

+1

@rym:あなたのXMLパーサーがDTDを読み込んでエンティティを解決しようとしていますが、上記のホストを解決できません。つまり、到達可能です)*または*問題のエンティティを解決するあなた自身の 'EntityResolver'を提供してください –

関連する問題