SimpleXML get tags sequence

111 views Asked by At

I have an model of object in xml file. This model has root tag and some tags inside it. I know how to read tags and parse it into POJO but how I can get sequence of tags?

For example:

<citation type="default">
    <part>first-author</part>
    <part>title</part>
    <part>type</part>
    <part>authors-after</part>
    <part>publisher</part>
    <part>editors</part>
    <part>publisher-city</part>
    <part>publisher-name</part>
    <part>year-date</part>
    <part>volume</part>
    <part>no</part>
    <part>pages</part>
</citation>

I need to read all tags inside in a queue so after i can read them one by one in same sequence as in xml.

1

There are 1 answers

0
davidxxx On BEST ANSWER

From the documentation : http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#list, I cooked you an example.

I have taken the idea here :

Reading a list of elements

In XML configuration and in Java objects there is often a one to many relationship from a parent to a child object. In order to support this common relationship an ElementList annotation has been provided.

Beware, I don't know this lib and I have not tested. Tell me if the result is not as expected.

Citation Class :

@Root
public class Citation{

   @ElementList
   private List<Part> list;

   @Attribute
   private String type;

   public String getType() {
      return type;
   }

   public List<Part> getList() {
      return list;
   }
}

Part class :

@Root
public class Part{

   @Text
   private String value;


   public String getValue() {
      return value;
   }
}

Deserializing your file :

Serializer serializer = new Persister();
File file = new File("yourXmlFile");
Citation citation = serializer.read(Citation.class, file);