Skip to content

Commit

Permalink
Brought the example up to date to make the current state of the JSON-…
Browse files Browse the repository at this point in the history
…binding work.
  • Loading branch information
bdoughan committed Mar 29, 2012
1 parent fd0e9f9 commit 325ff38
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,26 @@ To the following XML:

And the following JSON:

{"Placemark" :
{"AddressDetails" :
{"Country" :
{"CountryNameCode" : "US",
"AdministrativeArea" :
{"AdministrativeAreaName" : "CA",
"Locality" :
{"LocalityName" : "Mountain View",
"Thoroughfare" :
{"ThoroughfareName" : "1600 Amphitheatre Pkwy"},
"PostalCode" :
{"PostalCodeNumber" : "94043"}}}}}}}

{
"Placemark" : {
"AddressDetails" : {
"Country" : {
"CountryNameCode" : "US",
"AdministrativeArea" : {
"AdministrativeAreaName" : "CA",
"Locality" : {
"LocalityName" : "Mountain View",
"Thoroughfare" : {
"ThoroughfareName" : "1600 Amphitheatre Pkwy"
},
"PostalCode" : {
"PostalCodeNumber" : "94043"
}
}
}
}
}
}}

Compile the Example
-------------------
Expand Down
22 changes: 16 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@
<version>1.0</version>
<name>Map to Element based on an Attribute Value with EclipseLink JAXB (MOXy) </name>
<url>http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html</url>
<repositories>
<repository>
<id>EclipseLink Repo</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>
<repository>
<id>java.net</id>
<name>java.net Maven Repository</name>
<url>https://maven-repository.dev.java.net/nonav/repository</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.4.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>EclipseLink Repo</id>
<url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/blog/geocode/json/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@
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();
XMLInputFactory xif = XMLInputFactory.newInstance();
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");
unmarshaller.setProperty("eclipselink.json.include-root", false);
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.setProperty("eclipselink.json.include-root", false);
marshaller.marshal(addressFromJSON, System.out);
}

}

0 comments on commit 325ff38

Please sign in to comment.