Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法分析没有.xml扩展名的xml_Java_Android - Fatal编程技术网

Java 无法分析没有.xml扩展名的xml

Java 无法分析没有.xml扩展名的xml,java,android,Java,Android,我无法从扩展名不以.xml结尾的解析xml。但是,使用下面的代码,我可以在第一次保存到/res/raw时成功解析相同的xml 或者使用.xml扩展名(如或)创建 例如,“api.androidhive.info/pizza/?format=xml” 另外,在url末尾添加/?format=xml也不能解决问题 我认为这与获得HttpResponse有关,但我无法用我当前的代码解决这个问题。我需要能够检索xml并解析它,即使没有.xml扩展名 public String getXmlFro

我无法从扩展名不以.xml结尾的解析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)"}
07-25 17:54:39.090:I/INFO(29276):内容:{“消息”:“发生了错误”,“异常消息”:“值不能为空。\r\n参数名称:实体”,“异常类型”:“System.Data.entity.ModelConfiguration.Utilities.RuntimeFailureMethods.Requires”处的“System.ArgumentNullException”,“StackTrace”:“at System.Data.entity.ModelConfiguration.Utilities.RuntimeFailureMethods.Requires”(布尔条件,字符串userMessage,字符串conditionText)\r\n在System.Data.Entity.DbSet`1.Add(TEntity Entity)\r\n在iisCMS.Controller.GetAppsController.PostApp(App App)\r\n在lambda_方法(闭包,对象,对象[])处\r\n在System.Web.Http.Controllers.ReflectedHttActionDescriptor.ActionExecutor.c_uDisplayClass13.b_uC处(对象实例,对象[]方法参数)\r\n位于System.Web.Http.Controller.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象实例,对象[]参数)\r\n位于System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 Func,CancellationToken CancellationToken)}

当我检查来自服务器的响应时,它是
application/xml
,我想我需要
text/xml
任何方法将一个转换为另一个?

正如注释所示,现在您已经将正确的xml读入字符串变量
xml

首先将XML写入文件

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

然后,您还可以使用浏览器查看文件中的不规则情况。DTD、模式、名称空间、验证。

从Logcat发布日志。您确定要解析该xml吗?您在
xml
中得到了什么?显然不是xml,因此请检查响应是否正常(200),但可能需要重定向(302)。URL的帖子正确吗?没有获取?没有参数?@JoopEggen,日志中的信息是我在xml中得到的所有信息。试试看