English 中文(简体)
如果能够在XML或JSON中代表产出,那么该产品将使用什么
原标题:What API to use if the output could be represented in XML or JSON

http://www.viralheat.com 它可以将产出扔到Xml或json。 我应当使用什么样的java皮,以便我能够写出一个包裹,以便用JSON格式和以XML格式独立客户使用这一信息。

最佳回答

我建议使用Jackson ,因为它在生产JSON时优于其他推荐的替代品,而XML要么:

  • Use a JAXB implementation with JAXB annotations (Jackson can optionally support JAXB annotations as well, so very little overhead of 2 libs)
  • Use Jackson xml-databind module to produce XML using Jackson itself (in format that is usually identical to JAXB produced)

I do NOT think that you absolute must use a single library or framework, and the result is often better if you just choose best choice for each separately. XStream, for example, is good for XML, but produces ugly JSON (and slowly at that). Going from XML to JSON is error-prone due to format impedance and I have yet to see an XML-processing lib that produces JSON clean JSON efficiently (or even just clean or efficiently).

问题回答

我将使用Xstream支持XML和Jon绘制数据结构图。

<>说明: http://www.eclipse.org/eclipselink/moxy.php rel=“nofollow” EclipseLink JAXB (MOXy) Lead and a member of the JAXB 2 (JSR-222)。

您可以与MOXy合作,检查我的博客,其中使用一组元数据的一个领域模型,使用

下面是例子。 http://blog.bdoughan.com/201007/xpath-based-mapping.html MOXy s @XmlPath,分机,除日本宇宙航空研究开发机构说明外:

package blog.geocode.json;

import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
public class Address {

    @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
    private String street;

    @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:Locality/ns:LocalityName/text()")
    private String city;

    @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
    private String state;

    @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
    private String country;

    @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
    private String postalCode;

}

<>Demo

The standard JAXB APs are used to do the transformations to/ from JSON and XML:

package blog.geocode.json;

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Address.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // XML
        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource xml = new StreamSource("http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&sensor=false&key=YOUR_KEY_HERE");
        XMLStreamReader xsr = xif.createXMLStreamReader(xml);
        xsr.nextTag(); // Advance to kml tag
        xsr.nextTag(); // Advance to Response tag
        JAXBElement<Address> addressFromXML = unmarshaller.unmarshal(xsr, Address.class);
        marshaller.marshal(addressFromXML, System.out);

        // JSON
        unmarshaller.setProperty("eclipselink.media.type", "application/json");
        StreamSource json = new StreamSource("http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=json&sensor=false&key=YOUR_KEY_HERE");
        JAXBElement<Address> addressFromJSON = unmarshaller.unmarshal(json, Address.class);
        marshaller.setProperty("eclipselink.media.type", "application/json");
        marshaller.marshal(addressFromJSON, System.out);
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签