-1

I can't find a good example with google, so perhaps you know a tutorial for me:

I want to read an xml into java that will be nested like this

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <art>
    <name>x</name>
    <first>y</first>
    <alist>
        <set>1</set>
        <set>2</set>
        <set>3</set>
    </alist>
  </art>
  <art>
    <name>z</name>
    <first>a</first>
    <alist>
        <set>1</set>
        <set>2</set>
        <set>3</set>
    </alist>
  </art>
...
</config>

I can do that like http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/ but there are no additional tags like thos "set" ones. I'm a newby in this and I learn with examples.

Best regards,

Adreas

Rodrigo Guedes
  • 1,169
  • 2
  • 13
  • 27
Andreas Hornig
  • 2,439
  • 5
  • 28
  • 36

3 Answers3

1

I'll use the same answer from this post: How can I parse such a document?

A good practice is to use an object model that reflects your XML schema.

With that model, all what your parser have to do is to build the object during the parse process.

And with that you can also use some great frameworks build just to convert XML files to java objects, like XStream.

Community
  • 1
  • 1
Gilberto Torrezan
  • 5,113
  • 4
  • 31
  • 50
0

DOM parser, SAX parser or Pull parser are good to parse at smaller level, the bigger it gets it becomes harder to manage.....

Try using the below:

- JAXP and JAXB

- Castor

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

You could use JAXB (JSR-222) to read the XML into Java objects that you could process as you wish. An implementation of JAXB is included in the JDK/JRE starting with Java SE 6. Below is an example:

Config

JAXB is configuration by exception. This means you only need to add annotations where you want the XML representation to differ from the default (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html).

package forum12448687;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Config {

    private List<Art> art;

}

Art

package forum12448687;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Art {

    private String name;

    private String first;

    @XmlElementWrapper
    @XmlElement(name="set")
    private List<String> alist;

}

Demo

The code below demonstrates how to read the XML into object from and then write it back to XML.

package forum12448687;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Config.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum12448687/input.xml");
        Config config = (Config) unmarshaller.unmarshal(xml);

        Marshaller marshaller= jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(config, System.out);
    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config>
    <art>
        <name>x</name>
        <first>y</first>
        <alist>
            <set>1</set>
            <set>2</set>
            <set>3</set>
        </alist>
    </art>
    <art>
        <name>z</name>
        <first>a</first>
        <alist>
            <set>1</set>
            <set>2</set>
            <set>3</set>
        </alist>
    </art>
</config>
bdoughan
  • 147,609
  • 23
  • 300
  • 400