跳至主要内容

博文

目前显示的是 一月, 2017的博文

XSD Tutorial (3): Mis

In last blog , we have some problems and their solution, in this thread, we are going to solve more specific problems. Define id Like Attribute? < xs:attribute name = "id" type = "xs:ID" /> Define ref Like Attribute? < xs:attribute name = "ref" type = "xs:IDREF" /> Default Value for Element? Short answer is no, xsd doesn’t provide this functionality. Attributes can be set with a default value because it is relative simple, while elements can nested and combined, which is much more complex and hard to provide a default content. As far as we know, there also exists no alternatives to provide a default value for a missing element. In order to provide this functionality, we add much more code when combined with jaxb. Define key & keyref We define them in following steps: First, we must define key & keyref in element body; Second, define key in following format: < key id = ID

Validate and Parse XML in Java

This blog will introduce two main method in Java to parse xml and validate them. DOM DOM represent the document object model, which represents the xml file in tree like object. If you are familiar with js, you will be very familiar with this. In Java, the code looks different, but the core abstraction is still the same. The following code parse xml file and get Document object, then we can get the root element of the tree and analyze by ourselves. private void parseXML (String filename) throws IOException { DocumentBuilder db = getDocumentBuilder(); try { Document doc = db.parse( new File(filename)); // then handle your document through } catch (SAXException e) { // handle parse exception} } private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource" ; private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage" ; private s

JAXB Problem List (2)

This blog is the second part of previous blog , and we will cover more detailed problem here. Convert Elements to Map A very handy and useful feature in JAXB is the adapter. You can take some xml types and change them into your specific Java class. Here, we can change a xml element like following to a map: < A > < B > 1 </ B > < C > 2 </ C > ... </ A > public class MyAdapter extends XmlAdapter < MyAdapter . AdaptedMap , Map < String , Integer > { @XmlType (name = "A" ) public static class AdaptedMap { // represent the content in <A>...</A> @XmlAnyElement List<Element> elements = new ArrayList<>(); } @Override public Map<String,Integer> unmarshal (AdaptedMap adaptedMap) throws Exception { HashMap<String,Integer> map = new HashMap<>(); for (Element e : adaptedMap.elements) { map.put(e.getTagName(),

JAXB Problem List (1)

We have just have some xsd tutorials , now we use xsd we had and convert it to Java class to use. If you are not familiar with JAXB or similar framework (In my experience, I have written some ORM related code of EJB, like Hibernate, so I can understand how this might be like somewhat easily), you may want to skim oracle document first. Map Inheritance Relation We have already have the inheritance in xsd to reuse type, and we need to translate it Java code. < xs:complexType name = "parentType" > < xs:sequence > < xs:element name = "name" type = "xs:string" /> </ xs:sequence > </ xs:complexType > < xs:complexType name = "childType" > < xs:complexContent > < xs:restriction base = "parentType" > < xs:sequence > < xs:element name = "name" type = "specialStr" /> <

XSD Tutorial (2): Problem Lists

In last blog , we scan on some difficult part of XSD, in this tutorial, we continue with a problem lists to assist search. Problem Lists The following is the problems I met when working on it, and most answers are come from SO. Define Element With Optinal Body? < xs:element name = "query" minOccurs = "0" /> Define Enum? < xs:simpleType name = "dialects" final = "restriction" > < xs:restriction base = "xs:string" > < xs:enumeration value = "oracle" /> < xs:enumeration value = "inceptor" /> </ xs:restriction > </ xs:simpleType > Define Attributes Like maxOccurs We can notice that the value of macOccurs can be a non-negative number or a string – unbounded . How does it defined? The following is its definition: < xs:simpleType name = "allNNI" > < xs:union memberTypes = "xs:nonNegati

Things About Localhost

What Is Localhost When we refer to localhost in daily work, we always refer to the address of 127.0.0.1 . But do they really the same thing? The short answer is no. We can task localhost as a name mapping to some address (which is a little bit like domain name), so linux commonly map localhost to 127.0.0.1 in IPv4, to ::1 in IPv6. As we have stated, localhost is somewhat like a domain name, so it will be resolved by DNS system (often, this is done by a local hosts file as following shows). ~$ grep localhost /etc/hosts 127.0 . 0.1 localhost.localdomain localhost :: 1 localhost6.localdomain6 localhost6 How Is It Implemented We have already understand the concepts of localhost, but will it cost the network resources? Will it slow down the network? We will understand it if we know how it is implemented. When we send some data from application, those data often went through many layers of network protocols (application layer -> presentation layer -> sessio

XSD Tutorial (1): Startup

Recently, we have a system whose input is XML. In order to validate those input file, we decided to validate it using XSD , which is the shorthand of XML schema definition and is used to regulate XML format. I search the internet, but it seems that tutorials I found are not so complete, so I decided to write some problems I met to save future reader time. I recommend you to read this tutorial first (which is very short and it may just cost half an hour), understanding the basic things about xsd, then continue here. You can also find a problems list in the next blog. Basic Part Element vs Attribute vs Text < person gender = "female" > < firstname > Anna </ firstname > < lastname > Smith </ lastname > </ person > As above xml shows, person , firstname etc, the labels we use, are elements; gender , which in key value form, is attribute; Anna is the content/text of a element. Define Complex Type < xs:com