2009-06-23 8 views

答えて

5

SAX APIでJAXPを使用しました。 Xerces SAXParserのを必要とする

import org.xml.sax.*; 
import org.xml.sax.helpers.*; 
import javax.xml.parsers.*; 

.... 

InputStream in = connection.getInputStream(); 

InputSource responseXML = new InputSource(in); 
final StringBuilder response = new StringBuilder(); 
DefaultHandler myHandler = new DefaultHandler() { 

    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 
     if (localName.equals("elementname")) { 
      response.append(attributes.getValue("attributename")); 
      inElement = true; 
     } 
    } 
    public void characters(char [] buf, int offset, int len) { 
     if (inElement) { 
      inElement = false; 
      String s = new String(buf, offset, len); 
      response.append(s); 
      response.append("\n"); 
     } 
    } 
}; 

SAXParserFactory factory = SAXParserFactory.newInstance(); 
try { 
    SAXParser parser = factory.newSAXParser(); 
    parser.parse(responseXML, myHandler); 
} catch (ParserConfigurationException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (SAXException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

in.close(); 
connection.disconnect(); 

.... 
2

のXerces(つまり、SAXとDOM両方の実装を提供します)とのXalan(つまり、変換のためのサポートを提供) - の両方が1.5以降JDKに同梱されているので、すでに標準のJavaで設定されている

+1

App Engineが提供するJVMは、標準Javaインストールではありません。彼らが利用できるかどうか知っていますか? –

+0

こんにちはSam - 私はGoogle App Engineを使用していませんが、ドキュメントのクイックスキャンでは、セキュリティ上の制限付きのJava 6であることが示唆されています。ここをクリックしてください。http://code.google.com/appengine/docs/whatisgoogleappengine.html –

+0

Xercesは現在AppEngineではサポートされていません。セキュリティ上の問題。 –

2

をインストールするものをあなたの依存目標は私が推測している。私はJAXBをJavaオブジェクトへのXMLのマーシャリング/アンマーシャリングに使用しました。これはかなり高速で簡単に拡張でき、優れたコミュニティサポートを持っています。

スキーマを作成したくない場合は、dom4jで幸運を祈っています。学習曲線は小さくなります。

+0

Yuck、XMLを処理したい。私は実際に自分で書く必要はないので、JAXBは出ています。 –

+0

注釈を使用して、バインドするオブジェクトをマークアップすることもできます。私は個人的にはこのアプローチを使用していませんが、スキーマを書く必要はありません。私は、JAXBが多くのアプリケーションで容易に過度の攻撃を受ける可能性があることを認めます。 –

0

JDomは、標準のJava XML APIよりも優れた(より単純な)インタフェースを備えています。

0

サーブレット以外の環境で使用するライブラリとまったく同じライブラリを使用できます。

+0

LOL - 私はちょうど同じことを考えていた... –

+1

まあ、すべてがGAEで動作しない... – Thilo

0

Xerces(私がそれらを最後に比較したとき)よりも速い別の選択肢はSaxonでした。

2

あなたはJDOMを使用することができます。私のサーブレットに次のようなものを追加する

。ただし、AppEngineはxercesライブラリを提供していません。プロジェクトのWEB-INF/libフォルダーにコピーして追加することができます。

1
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

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


    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws IOException { 
     String content = req.getParameter("content"); 
     Document doc = parseXml(content); 
     resp.setContentType("text/plain"); 
     if (doc != null) 
     { 
      resp.getWriter().println(doc.getDocumentElement().getNodeName()); 
     } 
     else 
     { 
      resp.getWriter().println("no input/bad xml input. please send parameter content=<xml>"); 
     } 
    } 

    private static Document parseXml(String strXml) 
    { 
     Document doc = null; 
     String strError; 
     try 
     { 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 

      StringReader reader = new StringReader(strXml); 
      InputSource inputSource = new InputSource(reader); 

      doc = db.parse(inputSource); 

      return doc; 
     } 
     catch (IOException ioe) 
     { 
      strError = ioe.toString(); 
     } 
     catch (ParserConfigurationException pce) 
     { 
      strError = pce.toString(); 
     } 
     catch (SAXException se) 
     { 
      strError = se.toString(); 
     } 
     catch (Exception e) 
     { 
      strError = e.toString(); 
     } 

     log.severe("parseXml: " + strError); 
     return null; 
    } 
関連する問題