This blog posts extends Java and JAXB tutorial: Unmarshalling.
Q. Can you create your Java objects from XSDs?
A. Yes, you can by binding a schema. Binding a schema means generating a set of Java classes that represents the schema.
…
This blog posts extends Java and JAXB tutorial: Unmarshalling.
Q. Can you create your Java objects from XSDs?
A. Yes, you can by binding a schema. Binding a schema means generating a set of Java classes that represents the schema.
…
Q. What does JAXB stand for? What is an XML Binding?
A. JAXB means Java API for Xml Binding. XML binding maps an XML to in-memory Java objects. The principle advantage of using JAXB when marshaling and demarshaling XML is that is simplifies the programming model by allowing us to simply annotate a few POJOs and use the JAXB API’s and you can serialize to XML and deserialize from XML very easily.
…
Q. What is JSON and when to favor it over XML? A. JSON (JavaScript Object Notation) is a lightweight, text-based, language-netral like XML, but less verbose than XML data exchange format. JSOJ is used in Web services to exchange data between client and server. … Read more ›...
Q. What is a DOM parser, and when will you use it? A. The Java DOM API for XML parsing is intended for working with small to medium XML documents as an object graph in memory. The DOM parser traverses the XML file and creates the corresponding DOM objects linked together...
Q. What is a SAX Parser and when will you use it? A. SAX (Simple API for XML) is a stream oriented API like StAX, but Unlike StAX, which can read and write, but SAX can only read. … Read more ›...
Q. What is a StAX Parser and when will you use it?
A. The StAX Java API for XML processing is designed for parsing XML streams, just like the SAX API’s, but
…
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.xml; import java.math.BigDecimal; import org.springframework.core.style.ToStringStyler; public class Employee { private String name; private String type; private int age; private BigDecimal salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public BigDecimal getSalary() { return salary; } public void setSalary(BigDecimal salary) { this.salary = salary; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("name=" + name); sb.append("\n"); if(type != null) { sb.append("type=" + type + "\n"); } sb.append("age=" + age); return sb.toString(); } } |
Here is a simple example to write employee object to XML using simpler cursor based approach.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.xml; import java.io.StringWriter; import java.math.BigDecimal; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; public class StaxProcessing { public static void main(String[] args) { XMLOutputFactory factory = XMLOutputFactory.newInstance(); // construct employee object Employee emp = new Employee(); emp.setName("Peter"); emp.setType("first"); emp.setAge(25); emp.setSalary(BigDecimal.valueOf(35000.00)); StringWriter sw = new StringWriter(); try { XMLStreamWriter writer = factory.createXMLStreamWriter(sw); writer.writeStartDocument(); writer.writeStartElement("Employee"); writer.writeStartElement("name"); writer.writeAttribute("type", "first"); writer.writeCharacters(emp.getName()); writer.writeEndElement(); writer.writeStartElement("age"); writer.writeCharacters(new Integer(emp.getAge()).toString()); writer.writeEndElement(); writer.writeStartElement("salary"); writer.writeCharacters(emp.getSalary().toString()); writer.writeEndElement(); writer.writeEndElement(); System.out.println(sw.getBuffer().toString()); } catch (XMLStreamException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
Output:
|
1 2 3 |
<?xml version="1.0" ?> <Employee><name type="first">Peter</name><age>25</age><salary>35000.0</salary></Employee> |
Here is a simple example to … Read more ›...
Slf4j is indeed just a logging facade so that one can you can plug in his/her own logging frameworks like java.util.logging, log4j, Apache commons logging, and logback. Native implementation of slf4j is logback. Step 1: Create a new Maven based Java project.
|
1 |
mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simpleLog4j -DinteractiveMode=false |
Step 2: Import this project into eclipse...
BeanIO is an open source Java framework for marshaling and marshaling Java beans from a flat file, stream, or simple String object. It is very powerful with support for XML, CSV, delimited and fixed length stream formats, Object binding, filed validation, integration with spring-batch, etc. But there are scenarios where you need to simply read or write CSV data,
…
Step 1: Create the following folders
1. c:\projects\learn-java is the “project folder“.
2. com\mycompany1\main & com\mycompany1\service are the “packages” under the project.
…
This means handle a middle of an XML document (i.e. a node in middle of an XML), and use JAXB to marshal & unmarshal. The two key words are “JAXBElement” and “javax.xml.stream.XMLStreamReader” to accomplish this as shown below. Marshal a Car object to XML fragment Assume that “Car” … Read...
The javap is a class file dissembler. Here are 3 scenarios you can put javap to use with code examples.
#1 Finding the Java version of the class file within a jar
Extract a class file say “MyJobRunner.class” from a jar file and use javap to find out the version used
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
bash-3.2$ javap -v MyJobRunner Compiled from "MyJobRunner.java" public interface com.myapp.invoke.MyJobRunner SourceFile: "MyJobRunner.java" RuntimeVisibleAnnotations: length = 0x6 00 01 00 08 00 00 minor version: 0 major version: 51 Constant pool: const #1 = class #9; // com/myapp/invoke/MyJobRunner const #2 = class #10; // java/lang/Object const #3 = Asciz executeIlliquidDecsProcess; const #4 = Asciz ()V; const #5 = Asciz SourceFile; const #6 = Asciz MyJobRunner.java; const #7 = Asciz RuntimeVisibleAnnotations; const #8 = Asciz Lorg/springframework/stereotype/Service;; const #9 = Asciz com/myapp/invoke/MyJobRunner; const #10 = Asciz java/lang/Object; { public abstract void executeIlliquidDecsProcess(); } |
“major version: 51”
…
This extends Basic JAXB tutorial with Maven & XSD – 1 to marshall with Spring OXM and Spring XML.
Right mouse click on target/generated-sources and select “Build Path –> Use as Source Folder”
Step 1: add spring-orm to use JAXB and spring-xml to use StringResult.
…
I have a good news, I got job offer. I like to share with you from my bottom of my heart feeling thankful to you.