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

时间:2021-08-27 08:57:46

I have an XML file and I want to create a graph with some entities, then store this graph in an image, JPG or PNG.

我有一个XML文件,我想用一些实体创建一个图形,然后将这个图形存储在一个图像、JPG或PNG中。

So is there a library in Java do like this?? Or is there some tricks by parsing XML files and ... ???

那么Java中有这样的库吗?或者通过解析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:

输出是这样的:

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

3 个解决方案

#1


7  

You can extract the names using one of the myriad of Java XML libraries. Here's an example using XPath from a Java DOM:

您可以使用大量的Java XML库之一提取名称。下面是一个使用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 parsing will fail otherwise.

注意:这可能是一个错误,但是您的XML格式不太好——必须引用属性值。否则,XML解析将失败。

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

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);
}

#2


6  

I'd parse the XML and output a Graphviz DOT representation like this:

我将解析XML并输出如下所示的Graphviz点表示:

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

Then I'd call the Graphviz dot executable from Java using ProcessRunner:

然后我将使用ProcessRunner从Java调用Graphviz dot可执行文件:

dot -Tpng -o file.png file.dot

See http://graphviz.org for further info.

详情请参见http://graphviz.org。

#3


0  

Thanks everybody!

谢谢大家!

I found a class in java that's corporate with GraphViz to extraxt an image in the end after parsing the XML file and output a GraphViz representation, Follow this link. Well with the two methods of McDowell I can parse XML files easily.

我在java中发现了一个类,它与GraphViz相关联,在解析XML文件并输出一个GraphViz表示之后,在最后将图像从图像中提取出来,然后执行这个链接。用麦克道尔的两种方法,我可以很容易地解析XML文件。

Best regards

致以最亲切的问候

#1


7  

You can extract the names using one of the myriad of Java XML libraries. Here's an example using XPath from a Java DOM:

您可以使用大量的Java XML库之一提取名称。下面是一个使用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 parsing will fail otherwise.

注意:这可能是一个错误,但是您的XML格式不太好——必须引用属性值。否则,XML解析将失败。

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

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);
}

#2


6  

I'd parse the XML and output a Graphviz DOT representation like this:

我将解析XML并输出如下所示的Graphviz点表示:

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

Then I'd call the Graphviz dot executable from Java using ProcessRunner:

然后我将使用ProcessRunner从Java调用Graphviz dot可执行文件:

dot -Tpng -o file.png file.dot

See http://graphviz.org for further info.

详情请参见http://graphviz.org。

#3


0  

Thanks everybody!

谢谢大家!

I found a class in java that's corporate with GraphViz to extraxt an image in the end after parsing the XML file and output a GraphViz representation, Follow this link. Well with the two methods of McDowell I can parse XML files easily.

我在java中发现了一个类,它与GraphViz相关联,在解析XML文件并输出一个GraphViz表示之后,在最后将图像从图像中提取出来,然后执行这个链接。用麦克道尔的两种方法,我可以很容易地解析XML文件。

Best regards

致以最亲切的问候