How to get node attributes with Java SAX XML parser

Parsing XML from a String

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
 
public class Main {
 
  public static void main(String[] args) throws Exception {
    // Prepare input stream
    String text = "<person number=\"72\"><name>Benny</name></person>";
 
    // Read input stream
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(text)));
 
    // Output data
    NodeList elementsByTagName = document.getElementsByTagName("person");
    for (int i = 0; i &lt; elementsByTagName.getLength(); i++) {
      Node node = elementsByTagName.item(i);
      Element element = (Element) node;
      String attribute = element.getAttribute("number");
      System.out.println(attribute);
    }
  }
}

Parsing XML from a File

package com.welovecoding.parse.xml;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
 
public class Main {
 
  public static void main(String[] args) throws Exception {
    // Prepare input stream
    File file = new File("persons.xml");
    InputStream inputStream = new FileInputStream(file);
    Reader reader = new InputStreamReader(inputStream, "UTF-8");
    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");
 
    // Read input stream
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(is);
 
    // Output data
    NodeList elementsByTagName = document.getElementsByTagName("person");
    for (int i = 0; i < elementsByTagName.getLength(); i++) {
      Node node = elementsByTagName.item(i);
      Element element = (Element) node;
      String attribute = element.getAttribute("number");
      System.out.println(attribute);
    }
  }
}

Result

72

Create GraphML XML files that can be used in the yEd Graph Editor

In my article „Create GraphML XML file with Java“ I showed how to write a Graph to a GraphML-compatible XML file. Now I want to show you how you can write a GraphML-compatible file that can be used in the yEd Graph Editor to get visualized later.
Create GraphML XML files that can be used in the yEd Graph Editor weiterlesen

Parse some HTML tags with a Java SAX XML parser

Parsing XML with Java is very simple. If you want to parse some HTML tags, then you just have to add a root element around those tags (to make it a valid XML structure) and then you can use the Java SAX XML parser.

Code

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
 
public class NewMain {
 
  public static void main(String[] args) throws Exception {
    String html = "<h1>Headline</h1><p><b>Hello World.</b><b>This is a test.</b></p>";
    html = "<root>" + html + "</root>";
 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(html)));
    NodeList elementsByTagName = document.getElementsByTagName("b");
    for (int i = 0; i < elementsByTagName.getLength(); i++) {
      Node element = elementsByTagName.item(i);
      String text = element.getTextContent();
      System.out.println(text);
    }
  }
}

Result

Hello World.
This is a test.

Create GraphML XML file with Java

With Blueprints it is very easy to create a XML file following the GraphML standard. All you need is this:

import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.pgm.util.io.graphml.GraphMLWriter;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
public class App {
 
  private static final String OUTPUT_FILE = "./res/graph.graphml";
 
  public static void main(String[] args) throws Exception {
    OutputStream out = new FileOutputStream(OUTPUT_FILE);
    TinkerGraph graph = new TinkerGraph();
 
    Vertex source = graph.addVertex("1");
    Vertex target = graph.addVertex("2");
    graph.addEdge("3", source, target, "connection");
 
    GraphMLWriter writer = new GraphMLWriter(graph);
    writer.outputGraph(out);
  }
}

This is what you will get then:

graph.graphml

<?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
  <graph id="G" edgedefault="directed">
    <node id="2"></node>
    <node id="1"></node>
    <edge id="3" source="1" target="2" label="connection"></edge>
  </graph>
</graphml>

Export a GraphML graph as image with Java

In my post „How to parse GraphML files with a cool Java library“ I showed how to parse a GraphML XML file. In this post I want to show you, how you can visualize it. All you need is the yEd – Graph Editor and some extra markup in your XML file.
Export a GraphML graph as image with Java weiterlesen

How to parse GraphML files with a cool Java library

I’ve found this great GraphML reader and writer library: Blueprints. There is also a good documentation on how to use the GraphML library for reading XML-encoded graphs. Anyway, I think my example is better. 🙂
How to parse GraphML files with a cool Java library weiterlesen