简体   繁体   English

使用 Java 从 XML 文件创建图形图像(png、jpg..)

[英]Create a graph image (png, jpg ..) from an XML file with Java

I have an XML file and I want to create a graph based on some entities, then store this graph in an image, JPG or PNG.我有一个 XML 文件,我想基于一些实体创建一个图形,然后将此图形存储在图像、JPG 或 PNG 中。

So is there a library in Java to do something like this?那么Java中是否有一个库可以做这样的事情? Or are there some tricks by parsing XML files?或者解析 XML 文件有什么技巧吗?

Here an example XML file:这是一个示例 XML 文件:

<?xml version="1.0"?>
<process>
  <p n="1">Tove</p> 
  <p n="2">Jani</p> 
  <p n="2">Bill</p> 
  <p n="4">John</p> 
</process>

And the output will be like this:输出将是这样的:

在此处输入图像描述

You can extract the names using one of the myriad of Java XML libraries.您可以使用无数的 Java XML 库之一来提取名称。 Here's an example using XPath from a Java DOM :下面是一个使用Java DOM中的 XPath 的示例:

private static List<String> findNames(Document doc)
                                           throws XPathExpressionException {
  XPath xpath = XPathFactory.newInstance().newXPath();
  NodeList nodes = (NodeList) xpath.evaluate("/process/p", doc, 
                                                    XPathConstants.NODESET);
  List<String> names = new ArrayList<String>();
  for (int i = 0; i < nodes.getLength(); i++) {
    names.add(nodes.item(i).getTextContent());
  }
  return names;
}

Note: it may be a typo, but your XML is not well formed - attribute values must be quoted.注意:这可能是一个拼写错误,但您的 XML 格式不正确 - 必须引用属性值。 XML parsing will fail otherwise.否则 XML 解析将失败。

一些盒子

You can use the AWT API to draw whatever you want:您可以使用AWT API绘制任何您想要的东西:

private static final int BORDER = 1;
private static final int PADDING = 2;
private static final int SPACER = 5;

private static void draw(Graphics2D g, List<String> names) {
  FontMetrics metrics = g.getFontMetrics();
  Rectangle box = new Rectangle(1, 1, 0, 0);
  box.height = metrics.getHeight() + (PADDING * 2);
  g.setColor(Color.WHITE);
  for (String name : names) {
    box.width = metrics.stringWidth(name) + (PADDING * 2);
    g.drawString(name, box.x + BORDER + PADDING, PADDING + BORDER +
                                                    metrics.getHeight());
    g.drawRect(box.x, box.y, box.width, box.height);
    box.x += box.width + (BORDER * 2) + SPACER;
  }
}

This code just draws the names with some boxes around them.这段代码只是用周围的一些方框绘制名称。 I'm sure my offsets are all over the place, but you probably get the idea.我敢肯定我的抵消到处都是,但你可能明白了。

There is an imageio API that can save in a few popular data formats:有一个imageio API可以保存几种流行的数据格式:

private static void save(List<String> names, File file) throws IOException {
  BufferedImage image = new BufferedImage(600, 50, BufferedImage.TYPE_INT_RGB);
  Graphics2D g = image.createGraphics();
  try {
    draw(g, names);
  } finally {
    g.dispose();
  }
  ImageIO.write(image, "png", file);
}

I'd parse the XML and output a Graphviz DOT representation like this:我会解析 XML 并输出一个 Graphviz DOT 表示,如下所示:

digraph {
  Tove -> Jani
  Jani -> Bill
  Bill -> John
}

Then I'd call the Graphviz dot executable from Java using ProcessRunner:然后我会使用 ProcessRunner 从 Java 调用 Graphviz 点可执行文件:

dot -Tpng -o file.png file.dot

See http://graphviz.org for further info.有关详细信息,请参阅http://graphviz.org

I found a java class that works great with GraphViz to create an image after parsing the XML file and output a GraphViz representation.我找到了一个 java,它与GraphViz配合得很好,可以在解析 XML 文件并输出 GraphViz 表示后创建图像。 Follow this link for more information. 点击此链接了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM