我需要从数据库表中提取数据,并需要以下面的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>
Java类
@XmlRootElement(namespace = "com.abc.ROOT")
@XmlAccessorType(XmlAccessType.FIELD)
public class Rows implements Serializable {
private static final long serialVersionUID = 1;
@XmlAttribute
private int id;
@XmlAttribute
private String name;
@XmlAttribute
private String description;
@XmlAttribute
private int levelNumber;
@XmlAttribute
private Integer parentNodeId;
//If I have the following code, then rather than pulling the single id, it creates one more tree inside and displays all hierarchical structure for its parent node
//@XmlAttribute
//private Rows parentNodeId
}
使用上面的代码,我得到了随机排序的简单格式
<?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>
如何使用嵌套树结构等正确的排序获取XML中的树形图?我需要显示嵌套的树结构。
更新:12/10/2014。
当我使用以下更改修改了我的Row类时,我正在以revserse顺序获取树结构。即从孩子到父母。所以我为每个孩子打印了多行,一直指向父母
Java类
@XmlRootElement(namespace =“com.abc.ROOT”) @XmlAccessorType(XmlAccessType.FIELD)
public class Rows实现了Serializable {
private static final long serialVersionUID = 1;
@XmlAttribute
private int id;
@XmlAttribute
private String name;
@XmlAttribute
private String description;
@XmlAttribute
private int levelNumber;
**@XmlElement**
private **Rows** parentNodeId;
//If I have the following code, then rather than pulling the single id, it creates one more tree inside and displays all hierarchical structure for its parent node
//@XmlAttribute
//private Rows parentNodeId
}
在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>
如何首先使用Parent排序?
@lounce - 我按照上面的顺序从我的JPA类中获取数据。那么如何确定和添加子行呢?
答案 0 :(得分:1)
您的课程行不代表树形结构,因此您无法获得树。接下来的几个课程将更接近您的需要进行编组,但它并不完整。
@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, ... );
当然,您可以解释行元素中的id和父数据,以便正确排列树 - JAXB不会为我执行此操作; - )
这就是我得到的:
<tree>
<Row id="1">
<Row id="2">
<Row id="3"/>
</Row>
</Row>
<Row id="4"/>
</tree>
<强>后来强>
要根据parentId获取正确的链接,可以根据id-1将所有Node对象存储在数组中;让我们调用这个数组nodesById。然后:
for( Node node: nodesById ){
int pid = node.getParentId();
if (pid == 0){
tree.getNode().add( node );
} else {
nodesById[pid-1].getNode().add( node );
}
}