从java中的web服务调用解析xml
如果这是一个重复的线程,我深表歉意...
我有 ac# web 服务,我在其中返回一个 xml 对象,我需要在我的 android 应用程序中将其解析为 Hashmap,从服务返回的对象
大部分 是 .Net 类型字典其他调用返回 json,我可以使用响应字符串将其解析为 JsonObject,但此特定调用需要为其他应用程序返回 xml,
任何人都可以建议什么是将从 Web 服务返回的 xml 解析为 java 对象或在至少能够创建一个从响应的 xml 文档中提取值?
预先非常感谢
I apologise if this is a duplicate thread...
I have a c# web service where I return an xml object that I need to parse into a Hashmap in my android application, the returned object from the service is .Net type Dictionary
most of the other calls are returning json which I can parse into a JsonObject with the response string but this particular call needs to return xml for other apps
could anyone please advise whats the simplest way to go out parsing xml returned from a web service into java objects or at least be able to maybe create an xml document from the response and pull values out of that?
many thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要有两种方式,您需要根据您的要求选择一种方式。
1) SAX 解析器:主要用于读取 XML 文档中的所有元素并创建 java 对象。这里有一个参考。 SAX解析。这是一种内存消耗较少的方法,因为处理时不会缓存整个 xml 文件。
2) DOM 解析器:当您想要读取 xml 的特定部分时,这很有用(您可以使用它来解析整个文件,但当我需要处理 xml 的一部分时,我会使用更多)。这是更消耗内存的方法,因为在处理时整个 xml 文件将被缓存。这是一个例子。 Dom解析
你可以使用 Sun 内置 API 来实现这些解析机制(或者)还有其他第三方 API,如 Apache 等,
There are mainly two ways and based on your requirement you need to choose one.
1) SAX parser: Mainly useful if you want to read all elements in XML document and create java objects. Here is a referene. SAXParsing. This is less memory consuming approach because entire xml file won't be cached while processing.
2) DOM parser: This is useful when you want to read specific parts of the xml (you can use this to parse entire file, but I use more when I need to play with part of the xml). This is more memory consuming approach, because entire xml file will be cached while processing. Here is an example. Dom parsing
You may use Sun inbuilt API for these parsing mechanisms (or) there are other third party APIs like Apache etc.,
尝试 JAXB 或 简单XML
Try JAXB or SimpleXML