String filePath = "E:/桌面/cesgi.vsdx";
File file = new File(filePath);
XmlVisioDocument xmlVisioDocument = new XmlVisioDocument(new FileInputStream(file));
Collection<XDGFPage> pages = xmlVisioDocument.getPages();
Iterator<XDGFPage> iterator = pages.iterator();
//存储所有连线起点的集合
List<String> startLine = new ArrayList<>();
//存储所有连线起点的集合
List<String> endLine = new ArrayList<>();
//存储所有形状id和文字的集合
HashMap<String, String> idAndName = new HashMap<>();
//存储所有形状id和样式的集合
HashMap<String, String> idAndStyle = new HashMap<>();
//存储所有形状id和宽高的集合
HashMap<String, String> idAndSize = new HashMap<>();
while (iterator.hasNext()) {
XDGFPage xdgfPage = iterator.next();
XDGFPageContents content = xdgfPage.getContent();
//所有包含文字的形状节点
Collection<XDGFShape> shapes = content.getShapes();
Iterator<XDGFShape> iterator1 = shapes.iterator();
while (iterator1.hasNext()) {
XDGFShape xdgfShape = iterator1.next();
if (xdgfShape.hasText() && !xdgfShape.getText().getTextContent().equals("")) {
//存储文字
idAndName.put(Long.toString(xdgfShape.getID()), xdgfShape.getTextAsString());
//存储形状类型
idAndStyle.put(Long.toString(xdgfShape.getID()), xdgfShape.getName());
//存储形状数据,xml结构中*100为业务需要
idAndSize.put(Long.toString(xdgfShape.getID()), " <style x=\"" + Math.round(xdgfShape.getPinX() * 100) +
"\" y=\"" + Math.round(xdgfShape.getPinY() * 100) +
"\" width=\"" + Math.round(xdgfShape.getWidth() * 100) +
"\" height=\"" + Math.round(xdgfShape.getHeight() * 100) +
"\" as=\"style\"/>");
}
可以实现对vsdx文件的实时读取,解析,将数据转化为自定义xml