<?xml version="1.0" encoding="UTF-8"?>
<mxGraphModel>
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" vertex="1">
<Activity4Graph as="value" isLeaf="Y" name="活动1"
state="C" />
<mxGeometry as="geometry" height="70.0" width="100.0" x="290.0"
y="90.0" />
</mxCell>
<mxCell id="3" parent="1" vertex="1">
<Activity4Graph AId="7efcc9a6-9d7e-11df-9842-1d6abb6dee61"
as="value" isLeaf="N" name="活动2" state="C" />
<mxGeometry as="geometry" height="70.0" width="100.0" x="40.0"
y="190.0" />
</mxCell>
<mxCell edge="1" id="4" parent="1" source="2" style="" target="3">
<ActivityRelation4Graph as="value" name="连线3" />
<mxGeometry as="geometry" relative="1">
<mxPoint as="sourcePoint" x="360.0" y="145.0" />
<mxPoint as="targetPoint" x="140.0" y="260.0" />
</mxGeometry>
</mxCell>
<mxCell id="5" parent="1" vertex="1">
<Activity4Graph AId="936d24de-9dd9-11df-9842-1d6abb6dee61"
as="value" isLeaf="N" name="活动4" state="C" />
<mxGeometry as="geometry" height="70.0" width="100.0" x="270.0"
y="340.0" />
</mxCell>
<mxCell edge="1" id="6" parent="1" source="3" style="" target="5">
<ActivityRelation4Graph as="value" name="连线5" />
<mxGeometry as="geometry" relative="1">
<mxPoint as="sourcePoint" x="130.0" y="255.0" />
<mxPoint as="targetPoint" x="360.0" y="380.0" />
</mxGeometry>
</mxCell>
<mxCell id="7" parent="1" vertex="1">
<Activity4Graph as="value" isLeaf="Y" name="活动6"
state="C" />
<mxGeometry as="geometry" height="70.0" width="100.0" x="510.0"
y="260.0" />
</mxCell>
<mxCell edge="1" id="8" parent="1" source="5" style="" target="7">
<ActivityRelation4Graph as="value" name="连线7" />
<mxGeometry as="geometry" relative="1">
<mxPoint as="sourcePoint" x="350.0" y="385.0" />
<mxPoint as="targetPoint" x="580.0" y="330.0" />
</mxGeometry>
</mxCell>
<mxCell id="9" parent="1" vertex="1">
<Activity4Graph as="value" isLeaf="Y" name="活动8"
state="C" />
<mxGeometry as="geometry" height="70.0" width="100.0" x="490.0"
y="60.0" />
</mxCell>
<mxCell edge="1" id="10" parent="1" source="2" style="" target="9">
<ActivityRelation4Graph as="value" name="连线9" />
<mxGeometry as="geometry" relative="1">
<mxPoint as="sourcePoint" x="360.0" y="145.0" />
<mxPoint as="targetPoint" x="540.0" y="90.0" />
</mxGeometry>
</mxCell>
<mxCell id="A:3:R.12" parent="1" style="fillColor=#FFFFCC"
vertex="1">
<AARole4Graph aarID="9b8831d2-9d7e-11df-9842-1d6abb6dee61"
as="value" name="521_wa_5:wr1" />
<mxGeometry as="geometry" height="30.0" width="100.0" x="40.0"
y="260.0" />
</mxCell>
<mxCell id="A:3:I.13" parent="1" style="fillColor=#00FFCC"
vertex="1">
<Indicator4Graph as="value" graphID="A:3:I.13"
indicatorID="a1eb3199-9d7e-11df-9842-1d6abb6dee61" name="业务指标11" />
<mxGeometry as="geometry" height="10.0" width="10.0" x="40.0"
y="180.0">
<mxPoint as="offset" x="50.0" />
</mxGeometry>
</mxCell>
<mxCell id="11" parent="1" vertex="1">
<Activity4Graph as="value" isLeaf="Y" name="活动1"
state="C" />
<mxGeometry as="geometry" height="70.0" width="100.0" />
</mxCell>
</root>
</mxGraphModel>
解析代码
public class ReadXML {
public static Map<String, Location> maps;
static {
maps = new HashMap<String, Location>();
}
public static void main(String[] args) {
readXML("xml/demo.xml");
}
public static Map<String, Location> readXML(String path) {
Map<String, Location> mms= new HashMap<String, Location>();
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(loadResource(path));
List<Element> eleList = doc
.selectNodes("/mxGraphModel/root/mxCell");
for (Element ele : eleList) {
// String id = ele.valueOf("@id");
Location loc = new Location();
List<Element> Activity4Graphs = ele.elements("Activity4Graph");// 获取某节点下指定节点
int size = DataOptUtil.getSize(Activity4Graphs);
if (size > 0) {
for (Element element : Activity4Graphs) {
String name = element.valueOf("@name");
if (DataOptUtil.isNotNull(name)) {
loc.setNodeName(name);
}
}
}
List<Element> Indicator4Graphs = ele
.elements("Indicator4Graph");// 获取某节点下指定节点
int size1 = DataOptUtil.getSize(Indicator4Graphs);
if (size1 > 0) {
for (Element element : Indicator4Graphs) {
String name = element.valueOf("@name");
if (DataOptUtil.isNotNull(name)) {
loc.setNodeName(name);
}
}
}
List<Element> AARole4Graphs = ele.elements("AARole4Graph");// 获取某节点下指定节点
int size2 = DataOptUtil.getSize(AARole4Graphs);
if (size2 > 0) {
for (Element element : AARole4Graphs) {
String name = element.valueOf("@name");
if (DataOptUtil.isNotNull(name)) {
loc.setNodeName(name);
}
}
}
List<Element> ActivityRelation4Graphs = ele
.elements("ActivityRelation4Graph");// 获取某节点下指定节点
int size3 = DataOptUtil.getSize(ActivityRelation4Graphs);
if (size3 > 0) {
for (Element element : ActivityRelation4Graphs) {
String name = element.valueOf("@name");
if (DataOptUtil.isNotNull(name)) {
loc.setNodeName(name);
}
}
}
List<Element> mxGeometryS = ele.elements("mxGeometry");
for (Element element : mxGeometryS) {
if (DataOptUtil.isNotNull(element.valueOf("@width"))) {
double width = Double
.valueOf(element.valueOf("@width"));
loc.setWidth(width);
}
if (DataOptUtil.isNotNull(element.valueOf("@height"))) {
double height = Double.valueOf(element
.valueOf("@height"));
loc.setHeight(height);
}
if (DataOptUtil.isNotNull(element.valueOf("@x"))) {
double x = Double.valueOf(element.valueOf("@x"));
loc.setValueX(x);
}
if (DataOptUtil.isNotNull(element.valueOf("@y"))) {
double y = Double.valueOf(element.valueOf("@y"));
loc.setValueY(y);
}
List<Element> mxPoints = element.elements("mxPoint");// 获取某节点下指定节点
int size4 = DataOptUtil.getSize(mxPoints);
if (size4 > 0) {
for (Element e : mxPoints) {
if (DataOptUtil.isNotNull(e.valueOf("@x"))) {
double x = Double.valueOf(e.valueOf("@x"));
loc.setValueX(x);
}
if (DataOptUtil.isNotNull(e.valueOf("@y"))) {
double y = Double.valueOf(e.valueOf("@y"));
loc.setValueY(y);
}
}
}
}
if (loc.getNodeName()!=null) {
//System.out.println(loc);
mms.put(loc.getNodeName(), loc);
}
}
for (Entry<String, Location> entry : maps.entrySet()) {
System.out.println(entry.getKey() + "===" + entry.getValue());
}
} catch (DocumentException e) {
e.printStackTrace();
}
return mms;
}
public static InputStream loadResource(String path) {
return Thread.currentThread().getContextClassLoader()
.getResourceAsStream(path);
}
}
涉及到的Location类
public class Location {
private String nodeName;
private double width;
private double height;
private double valueX;
private double valueY;
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getValueX() {
return valueX;
}
public void setValueX(double valueX) {
this.valueX = valueX;
}
public double getValueY() {
return valueY;
}
public void setValueY(double valueY) {
this.valueY = valueY;
}
@Override
public String toString() {
return "Location [height=" + height + ", nodeName=" + nodeName + ", valueX="
+ valueX + ", valueY=" + valueY + ", width=" + width + "]";
}
}