Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 引用其父级的JAXB树结构格式化不起作用_Java_Xml_Jaxb_Marshalling_Unmarshalling - Fatal编程技术网

Java 引用其父级的JAXB树结构格式化不起作用

Java 引用其父级的JAXB树结构格式化不起作用,java,xml,jaxb,marshalling,unmarshalling,Java,Xml,Jaxb,Marshalling,Unmarshalling,我需要从数据库表中获取数据,并需要以下面的XML树格式显示它 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ROOT> <Rows> <Row id =1 name=" " desc="" parentId="0"> <Row id = 2 name="" desc="" parentId="1"

我需要从数据库表中获取数据,并需要以下面的XML树格式显示它

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ROOT>
        <Rows>
            <Row id =1 name=" " desc="" parentId="0">
               <Row id = 2 name="" desc="" parentId="1" >
                    <Row id = 3 name="" desc="" parentId="2" />
            </Row>
            <Row id = 4 name=" " desc="" parentId="0" />
        </Rows>
    </ROOT>
通过上面的代码,我得到了随机排序的普通格式

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT>
    <Rows>
        <Row id = 3 name="" desc="" parentId="2" />
        <Row id = 1 name=" " desc="" parentId="0"/>
        <Row id = 4 name=" " desc="" parentId="0" />
        <Row id = 2 name="" desc="" parentId="1"/>
    </Rows>
</ROOT>
在Hibernate文件中,我定义了多对一关系,其中PARENT_ID是外键

我要走了

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ROOT>
        <Rows>
            <Row id = 3 name="" desc="" parentId="2" >
               <Row id = 2 name="" desc="" parentId="1">
                   <Row id = 1 name=" " desc="" parentId="0"/>
               </Row>
            <Row>
            <Row id = 4 name="" desc="" parentId="1" >
                   <Row id = 1 name=" " desc="" parentId="0"/>
               </Row>
        </Rows>
    </ROOT>

我怎样才能先和家长一起把它分类呢


@lounce-我从我的JPA类中按上述顺序获取数据。那么我应该如何确定和添加子行呢?

您的类行不代表树结构,因此您不会得到树。下面的两个类将更接近您所需的封送,但它并不完整

@XmlRootElement
public class Tree {
    @XmlElement( name = "Row" )
    private List<Node> node;
    public Tree(){}
    public List<Node> getNode(){ 
        if( node == null ) node = new ArrayList<>();
        return node; 
    }
}

public class Node {
    private int id;
    @XmlElement(name="Row")
    private List<Node> node;
    public Node( int id ){
        this.id = id;
    }
    @XmlAttribute
    public int getId(){ return id; }
    public void setId( int value ){ id = value; }
    public List<Node> getNode(){ 
    if( node == null ) node = new ArrayList<>();
        return node; 
    }
}
当然,解释行元素中的id和父数据是由您自己决定的,以便正确排列树-JAXB和我都不会为您这样做;-)

这就是我得到的:

<tree>
    <Row id="1">
        <Row id="2">
            <Row id="3"/>
        </Row>
    </Row>
    <Row id="4"/>
</tree>

有人能提供建议吗?谢谢你的回答@launeI我用更新的更改再次更新了我的问题这是一个选择正确的节点对象以插入节点的单一问题。请参阅我的A中的新增内容。谢谢Laune,这很有帮助,我能够获得我想要的XML格式。谢谢你的帮助。
@XmlRootElement
public class Tree {
    @XmlElement( name = "Row" )
    private List<Node> node;
    public Tree(){}
    public List<Node> getNode(){ 
        if( node == null ) node = new ArrayList<>();
        return node; 
    }
}

public class Node {
    private int id;
    @XmlElement(name="Row")
    private List<Node> node;
    public Node( int id ){
        this.id = id;
    }
    @XmlAttribute
    public int getId(){ return id; }
    public void setId( int value ){ id = value; }
    public List<Node> getNode(){ 
    if( node == null ) node = new ArrayList<>();
        return node; 
    }
}
    Tree tree = new Tree();
    Node n1 = new Node(1);
    tree.getNode().add( n1 );
    Node n2 = new Node(2);
    n1.getNode().add( n2 );
    Node n3 = new Node(3);
    n2.getNode().add( n3 );
    Node n4 = new Node(4);
    tree.getNode().add( n4 );
    JAXBContext jc = JAXBContext.newInstance( Tree.class );
    Marshaller m = jc.createMarshaller();
    m.marshal( tree, ... );
<tree>
    <Row id="1">
        <Row id="2">
            <Row id="3"/>
        </Row>
    </Row>
    <Row id="4"/>
</tree>
for( Node node: nodesById ){
    int pid = node.getParentId();
    if (pid == 0){
       tree.getNode().add( node );
    } else {
       nodesById[pid-1].getNode().add( node );
    }
}