Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Evaluate XPath Expression in XML File Using Java?

Evaluate XPath Expression in XML

Condition for Evaluating XPath Expression in XML File Using Java

  • Description: Evaluating an XPath expression in an XML file using Java can be accomplished through the use of the XPath API, which is part of the `javax.xml.xpath` package. First, an XPathFactory object is created to instantiate an XPath object. The XML file is parsed into a Document object using a DocumentBuilder. Once the document is available, an XPath expression is compiled into an XPathExpression object. The expression is evaluated against the XML document using the `evaluate()` method of the XPath object, which returns the result of the evaluation, typically as a `String`, `NodeList`, or `Boolean`, depending on the type of XPath expression. To handle the XML file, the `FileInputStream` can be used to read the XML file, and the result can be processed accordingly. Error handling is essential to manage invalid XPath expressions or issues during document parsing.
Sample Source Code
  • # XpathEvaluate.java
    package JavaSamples;

    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    public class XpathEvaluate extends javax.swing.JFrame {

    public XpathEvaluate() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Evaluate XPath Expression in XML File");

    jLabel2.setText("File Path");

    jButton1.setText("Choose xml File");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jButton2.setText("Evaluate");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton2ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
    .addGap(0, 0, Short.MAX_VALUE)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(79, 79, 79))
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(25, 25, 25)
    .addComponent(jLabel2)
    .addGap(27, 27, 27)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addGap(130, 130, 130)
    .addComponent(jButton1))
    .addGroup(layout.createSequentialGroup()
    .addGap(74, 74, 74)
    .addComponent(jLabel1))
    .addGroup(layout.createSequentialGroup()
    .addGap(170, 170, 170)
    .addComponent(jButton2)))
    .addContainerGap(65, Short.MAX_VALUE))
Screenshots
  • STEP 1: The user selects an XML file from local files.
  • The user selects an XML file from local files.

  • STEP 2: To click the choose XML file button, it navigates the local device to choose a file.
  • To click the choose XML file button, it navigates the local device to choose a file.

  • STEP 3: The file path will be displayed in the text field.
  • The file path will be displayed in the text field.

  • STEP 4: The user selects the Evaluate button to evaluate the XPath expression and retrieve book titles, displaying the titles in the JTextArea, while handling any exceptions that occur during the process.
  • The user selects the Evaluate button to evaluate the XPath expression and retrieve book titles, displaying the titles in the JTextArea.

  • STEP 5:Xml file
  • Xml file