0

.xml 拡張子で終わらないURLから xml を解析できません。ただし、以下のコードを使用すると、最初に /res/raw に保存したときに同じ xml を正常に解析できます。

または、このような .xml 拡張子で作成された場合、または「api.androidhive.info/pizza/?format=xml」など。

また、URL の末尾に追加/?format=xmlしても問題は解決しません。

これは HttpResponse の取得と関係があると思いましたが、現在のコードでは問題を解決できません。.xml 拡張子がなくても、xml を取得して解析できる必要があります。

    public String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
}

私のlogcatの結果は次のとおりです。

07-25 17:54:39.090: I/INFO(29276): Content:{"Message":"An error has occurred.","ExceptionMessage":"Value cannot be null.\r\nParameter name: entity","ExceptionType":"System.ArgumentNullException","StackTrace":"   at System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.Requires(Boolean condition, String userMessage, String conditionText)\r\n   at System.Data.Entity.DbSet`1.Add(TEntity entity)\r\n   at iisCMS.Controllers.GetAppsController.PostApp(App app)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}

サーバーからの応答を確認したところ、それはそうでしたが、一方を他方に変換する方法がapplication/xml必要だと思いますか?text/xml

4

1 に答える 1

0

コメントが示すように、これで正しい XML を String 変数に読み込みましたxml

最初に XML をファイルに書き込みます。

Path path = new Path("C:/Temp/test.xml");
Files.write(file, xml.getBytes("UTF-8"),
    StandardOpenOption.WRITE | StandardOpenOption.CREATE);

次に、ブラウザを使用して、ファイルに異常がないか調べることができます。DTD、スキーマ、名前空間、検証。

于 2013-07-25T12:59:29.480 に答える