如何解析包含XML结构的字段?

时间:2011-07-12 08:53:34

标签: java mysql xml

我的数据库(MYSQL)上有一个包含XML结构的字段,我想解析该字段以提取数据,我的想法是提取该字段并将其复制到文件中然后解析它, 但是我应该为超过200行做这个问题(而且这个数字在增长的时候),这意味着我应该每次打开一个文件复制其中的字段,消耗时间和内存,是他们的另一个溶液

更新 我试过这段代码,但它产生了一个错误:

if(var2.length()!=0)
             {
            System.out.println("---------------test1--------------");

             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
                System.out.println("*********test2*********");
                InputSource is = new InputSource(new StringReader(var2)); 
                Document parse = builder.parse(is);
                System.out.println("builder.parse"+builder.parse(is).toString());
    System.out.println("______test3_____");


             }
             else
                    System.out.println("null");

        }


             }
             else
                    System.out.println("null");

错误:

---------------test1--------------
*********test2*********
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:88)

2 个答案:

答案 0 :(得分:1)

你使用什么解析器? 您不必将内容保存到文件(即SAX):

saxParser.parse(new InputSource(new StringReader(yourXMLstring));

编辑:当然,如果可能,请事先将xml数据导入正确的关系方案,如Quentin所述。

答案 1 :(得分:0)

不要使用XML。改为使用数据库表来表示数据。