Menu

Home

Nikolay Genov

Java XML parser. Automatic. Just Java Classes with fields. No code.

See the examples in src/test/java/baba/tonka/
ProcessXmlTest.java is the simple one, and
ServletContextTest.java the more complicated one, with XML namespaces etc.

  1. Basicliy if you have a xml like this

~~~~~~~~

<cart>
<number>alfa123</number>
<cartcolor shade="gray">blue</cartcolor> </cart>

<!-- multiple nested elements-->
<product type="complex">
    <name id="k123">krusha123</name>
    <weight>10</weight>
</product>
<product type="complex">
    <name id="b123">banan123</name>
    <weight>5</weight>
</product>
<product type="complex">
    <name>no_id</name>
    <weight>5</weight>
</product>
<product type="no_name_and_id_but_hasText">I am a product
    <name></name>
    <weight>5</weight>
</product>

* You define a Java classes like this

~~~~

public class Cart implements IXmlElement {
    public XmlTextElement number;
    public CartColor cartColor;
    public Product[] product;


    public static class Product extends XmlTextElement {
        public String type;//attribute
        public Name name;
        public XmlTextElement weight;//just text
    }

    public static class Name extends XmlTextElement {
        public String id;
    }
}
~~~~

~~~~
public class CartColor extends XmlTextElement {
    public String shade;//attribute
}


~~~~

* call a parser like this
    Cart cart = new Cart();
    ProcessXml.fromJdomElement(doc.getRootElement(), cart);
    assertEquals("alfa123", cart.number._mgXmlTextValue);
    assertEquals("blue", cart.cartColor._mgXmlTextValue);
and you have the the values from XML usable in Java (in the form of fields)

* You can also do the reverse and produce XML strings like this
    Cart cart = new Cart();
    ProcessXml.fromJdomElement(doc.getRootElement(), cart);

    Element jdomElement = ProcessXml.toJdomElement("cart", cart);
    System.out.println("processed no namaspace xml \n" + new XMLOutputter(Format.getPrettyFormat()).outputString(jdomElement));

~~~~~~~

This is the default page, edit it as you see fit. To add a new page simply reference it within brackets, e.g.: [SamplePage].

The wiki uses Markdown syntax.

Project Members: