Coder Social home page Coder Social logo

uia-xml's Introduction

uia-xml

Description

Map XML document to objects using StAX API.

δΈ­ζ–‡

Annotations

The annotations used to define XML document includes

  • @TagInfo
  • @AttrInfo
  • @PropInfo
  • @ContentInfo
  • @XmlInfo
  • @TagListInfo
  • @TagListElem

@TagInfo

Define an element.

Properties:

  • name - the element name.

Examples:

<Book />
@TagInfo(name = "Book")
public class Book {}

@AttrInfo

Define an attribute of an element. Value type supports

  • long
  • int
  • short
  • byte
  • boolean
  • double
  • float
  • String
  • BigDecimal

Properties:

  • name - the attribute name.
  • parser - the value parser.

Examples:

<Book id="abc" name="API Toturial" />
@TagInfo(name = "Book")
public class Book {

    @AttrInfo
    private String id;

    @AttrInfo(name = "name")
    private String bookName;
}

@PropInfo

Simple element, Value type supports

  • long
  • int
  • short
  • byte
  • boolean
  • double
  • float
  • String
  • BigDecimal

Properties:

  • name - the element name.
  • parser - the value parser.
  • cdata - if the content is CDATA or not.

Examples:

<Book id="abc" name="API Toturial" />
    <author>Kyle</author>
    <price>100</price>
    <hardback>true</hardback>
</Book>
@TagInfo(name = "Book")
public class Book {

    @AttrInfo
    private String id;

    @AttrInfo(name = "name")
    private String bookName;

    @PropInfo(name = "author")
    private String authorName;

    @PropInfo
    private int price;

    @PropInfo
    private boolean hardback;
}

@ContentInfo

Contnet of an element. This will be used when a XML element has attributes and text. Value type supports

  • long
  • int
  • short
  • byte
  • boolean
  • double
  • float
  • String
  • BigDecimal

Properties:

  • parser - the value parser.
  • cdata - if the content is CDATA or not.

Exammples:

<Book id="abc" name="API Toturial" />
    <author>Kyle</author>
    <price>100</price>
    <hardback>true</hardback>
    <release time="2022-10-26">Good</release>
</Book>
@TagInfo(name = "Book")
public class Book {

    @AttrInfo
    private String id;

    @AttrInfo(name = "name")
    private String bookName;

    @PropInfo(name = "author")
    private String authorName;

    @PropInfo
    private int price;

    @PropInfo
    private boolean hardback;

    private @TagInfo
    private Release release;
}

@TagInfo
public class Release {

    @AttrInfo
    private String time;

    @ContentInfo
    private String text;
}

@XmlInfo

The content is XML fommat.

Properties:

  • name - the element name.

Exammples:

<Lib>
    <Books>
        <Book id="abc" name="API Toturial" />
            <author>Kyle</author>
            <price>100</price>
            <hardback>true</hardback>
            <release time="2022-10-26">Good</release>
        </Book>
    </Books>
</Lib>
@TagInfo(name = "Lib")
public class Lib {

    @XmlInfo(name = "Books")
    private String books;
}

After reading XML document, the text stored in the books will be

<Book id="abc" name="API Toturial" /><author>Kyle</author><price>100</price><hardback>true</hardback><release time="2022-10-26">Good</release></Book>

@TagListInfo

List element.

Properties:

  • @TagListInfo

    • name - the element name.
    • elems - definition of sub-elements in the list. Array of @TagListElem.
    • inline - if element exists or not.
  • @TagListElem

    • name - the element name.
    • type - the class.

examples:

  1. inline style

    <Lib>
        <Book />
        <Book />
        <Book />
    </Lib>
    @TagInfo(name = "Lib")
    public class Lib {
    
        @TagListInfo(elems = { @TagListElem(name = "Book", type = Book.class) }, inline = true)
        private ArrayList<Book> values;
    
    }
  2. NOT inline style

    <Lib>
        <Books>
            <Book />
            <Book />
            <Book />
        </Books>
    </Lib>
    @TagInfo(name = "Lib")
    public class Lib {
    
        @TagListInfo(name = "Books", elems = { @TagListElem(name = "Book", type = Book.class) })
        private ArrayList<Book> books;
    
    }
  3. list with mulitple types.

    <Zoo>
        <Animals>
            <Tiger id="0001" />
            <Lion id="0002" />
            <Lion id="0003" />
            <Tiger id="0004" />
            <Lion id="0005" />
        </Animals>
    </Zoo>
    @TagInfo(name = "Zoo")
    public class Zoo {
    
        @TagListInfo(
            name = "Animals", 
            elems = { 
                @TagListElem(name = "Tiger", type = Tiger.class) 
                @TagListElem(name = "Lion", type = Lion.class) 
            })
        private ArrayList<Animal> animals;
    
    }

Value Parser

@AttrInfo, @PropInfo and @Content support parser configurtion. The parser class needs to implement uia.xml.XObjectValue interface.

For example, the type of result element in the class is boolean, and the XML content is Y or N.

<result>Y</result>
@PropInfo(parser = BooleanValue.class)
private boolean result;
public class BooleanValue implements XObjectValue {

    public Object read(Field f, String text) {
        return "Y".equals(text);
    }

    public String write(Object value) {
        boolean b = (Boolean)value;
        return b ? "Y" : "N";
    }
}

Copyright and License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

uia-xml's People

Contributors

gazer2kanlin avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.