Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3870800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:45:46+00:00 2026-05-19T21:45:46+00:00

I have an XML file and I want to create a graph based on

  • 0

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.

So is there a library in Java to do something like this? Or are there some tricks by parsing XML files?

Here an example
XML file:

<?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:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T21:45:46+00:00Added an answer on May 19, 2026 at 9:45 pm

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

    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.

    some boxes

    You can use the AWT API to draw whatever you want:

    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:

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
I have an xml file providing data for a datagrid in Flex 2 that
I have an xml file where I need to comment out a whole piece
I have an XML file loaded into a DOM document, I wish to iterate
I have an XML file and an XML schema in another file and I'd
I have a XML File like that <?xml version=1.0 encoding=utf-8 ?> <Configurations> <EmailConfiguration> <userName>xxxx</userName>
I have an XML file with the following line: <VALUE DECIMAL_VALUE=0.2725 UNIT_TYPE=percent/> I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.