java - Missing segments when reading XML file - Stack Overflow

I am working on a demo application to read an XML file. When reading the XML file I am noticing a few X

I am working on a demo application to read an XML file. When reading the XML file I am noticing a few XML segments are missing on the java object.

Here is a sample XML I am using:

<person xid="PERSON_LOOP" type="explicit">
    <name>Person Loop Information</name>
    <segment xid="PER">
        <name>Person Header-1</name>
    </segment>
    <person xid="CHILD_LOOP" type="explicit">
        <name>Child Loop Information</name>
        <segment xid="CS">
            <name>Child Header</name>
        </segment>
    </person>
    <segment xid="PER1">
        <name>Person Header-2</name>
    </segment>
    <segment xid="PER2">
        <name>Person Header-3</name>
    </segment>
</person>

My java classes are:

LoopDefinition:

import java.util.List;
    
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JacksonXmlRootElement(localName = "person")
public class LoopDefinition  {

    @JacksonXmlProperty(isAttribute = true, localName = "xid")
    private String xid;

    @JacksonXmlProperty(localName = "name")
    private String name;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "segment")
    private List<SegmentDefinition> segments;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "person")
    private List<LoopDefinition> loops;
}

Segment Definition:

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JacksonXmlRootElement(localName = "segment")
public class SegmentDefinition {

    @JacksonXmlProperty(isAttribute = true, localName = "xid")
    private String xid;

    @JacksonXmlProperty(localName = "name")
    private String name;

}

After reading the XML file, I noticed that the object is missing segment id PER in the segment list and after debugging I found that the PER segment which was first read to the SegmentDefinition list is being overwritten when subsequent segments on the parent level are read. i.e., PER is being replaced by PER-1 and PER-2 instead of giving PER, PER-1 and PER-2.

Here is a output from the loopDefinition object:

LoopDefinition(xid=PERSON_LOOP, name=Person Loop Information, segments=[SegmentDefinition(xid=PER1, name=Person Header-2), SegmentDefinition(xid=PER2, name=Person Header-3)], loops=[LoopDefinition(xid=CHILD_LOOP, name=Child Loop Information, segments=[SegmentDefinition(xid=CS, name=Child Header)], loops=null)])

It is missing SegmentDefinition(xid=PER, name=Person Header-1) in the SegmentDefinition list.

Please let me know if I am missing something. Thanks in advance for the help!

I am working on a demo application to read an XML file. When reading the XML file I am noticing a few XML segments are missing on the java object.

Here is a sample XML I am using:

<person xid="PERSON_LOOP" type="explicit">
    <name>Person Loop Information</name>
    <segment xid="PER">
        <name>Person Header-1</name>
    </segment>
    <person xid="CHILD_LOOP" type="explicit">
        <name>Child Loop Information</name>
        <segment xid="CS">
            <name>Child Header</name>
        </segment>
    </person>
    <segment xid="PER1">
        <name>Person Header-2</name>
    </segment>
    <segment xid="PER2">
        <name>Person Header-3</name>
    </segment>
</person>

My java classes are:

LoopDefinition:

import java.util.List;
    
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JacksonXmlRootElement(localName = "person")
public class LoopDefinition  {

    @JacksonXmlProperty(isAttribute = true, localName = "xid")
    private String xid;

    @JacksonXmlProperty(localName = "name")
    private String name;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "segment")
    private List<SegmentDefinition> segments;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "person")
    private List<LoopDefinition> loops;
}

Segment Definition:

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JacksonXmlRootElement(localName = "segment")
public class SegmentDefinition {

    @JacksonXmlProperty(isAttribute = true, localName = "xid")
    private String xid;

    @JacksonXmlProperty(localName = "name")
    private String name;

}

After reading the XML file, I noticed that the object is missing segment id PER in the segment list and after debugging I found that the PER segment which was first read to the SegmentDefinition list is being overwritten when subsequent segments on the parent level are read. i.e., PER is being replaced by PER-1 and PER-2 instead of giving PER, PER-1 and PER-2.

Here is a output from the loopDefinition object:

LoopDefinition(xid=PERSON_LOOP, name=Person Loop Information, segments=[SegmentDefinition(xid=PER1, name=Person Header-2), SegmentDefinition(xid=PER2, name=Person Header-3)], loops=[LoopDefinition(xid=CHILD_LOOP, name=Child Loop Information, segments=[SegmentDefinition(xid=CS, name=Child Header)], loops=null)])

It is missing SegmentDefinition(xid=PER, name=Person Header-1) in the SegmentDefinition list.

Please let me know if I am missing something. Thanks in advance for the help!

Share Improve this question edited Mar 14 at 18:34 Vamsi asked Mar 14 at 17:49 VamsiVamsi 7013 gold badges12 silver badges26 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Based on this bug report, you can not arbitrarily split elements of an array around other properties. But there is a workaround, you have to override the setter method of segments list as follows:

@Getter
@Setter
@ToString
@JacksonXmlRootElement(localName = "person")
static class LoopDefinition  {

    @JacksonXmlProperty(isAttribute = true, localName = "xid")
    private String xid;

    @JacksonXmlProperty(localName = "name")
    private String name;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "segment")
    private List<SegmentDefinition> segments;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "person")
    private List<LoopDefinition> loops;

    
    // overrides lombok setter
    public void setSegments(List<SegmentDefinition> value){
        if (segments == null){
            segments = new ArrayList<>();
        }
        segments.addAll(value);
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744638689a4585303.html

相关推荐

  • java - Missing segments when reading XML file - Stack Overflow

    I am working on a demo application to read an XML file. When reading the XML file I am noticing a few X

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>