0

I have an application with an xml like this :

<root>
<info>
.....
</info>
 <type>
   <object_type>..</object_type>
    <prop>
      <data>
        .... . .. 
    </prop>
    <param>
     ....
    </param>
     <param>
     ....
    </param>
    <param>
     ....
    </param>

etc

All this data is parsed from XML and now I the user should be able to edit the data. So I have implemented a method for updating the params:

but its giving me an exception due to wrongly written Xpath statement :

javax.xml.xpath.XPathExpressionException
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source)
    at xmleditor.service.XMLEditorService.updateParameters(XMLEditorService.java:96)
    at xmleditor.gui.XmlEditorMain$5$1.tableChanged(XmlEditorMain.java:364)
    at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
    at javax.swing.table.AbstractTableModel.fireTableCellUpdated(Unknown Source)
    at javax.swing.table.DefaultTableModel.setValueAt(Unknown Source)
    at javax.swing.JTable.setValueAt(Unknown Source)
    at javax.swing.JTable.editingStopped(Unknown Source)
    at javax.swing.AbstractCellEditor.fireEditingStopped(Unknown Source)
    at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(Unknown Source)
    at javax.swing.DefaultCellEditor.stopCellEditing(Unknown Source)
    at javax.swing.JTable$GenericEditor.stopCellEditing(Unknown Source)
    at javax.swing.DefaultCellEditor$EditorDelegate.actionPerformed(Unknown Source)
    at javax.swing.JTextField.fireActionPerformed(Unknown Source)
    at javax.swing.JTextField.postActionEvent(Unknown Source)
    at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


 Caused by: javax.xml.transform.TransformerException: Ett platssteg förväntades efter token  '/' eller '//'.
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
    ... 41 more

So what I have try to do is the get the rowCount from the table that the user edit and store it in an int x. Then I have stored the columnName in an String columnName. And I am trying to use this variables in my xpath expression to find the perfect location depending on the ObjectType the user has selected. and update and save it in the xml file. But somehow it seems that my xpath expression is wrong or that the transformer cannt read it somehow. since I also get this one :

Caused by: javax.xml.transform.TransformerException

this is my java code:

// Update the parameters
    public void updateParameters(String columnName, Object data, int x,
            Type type) throws ParserConfigurationException, SAXException,
            IOException, XPathExpressionException {

        File file = new File("xmlFiles/CoreDatamodel.xml");
        System.out.println("Incoming String value : " + data);
        System.out.println("Index value : " + x);

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                .newInstance();

        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        Document xmlDocument = builder.parse(file);

        XPath xPath = XPathFactory.newInstance().newXPath();

        Node node = (Node) xPath.compile(
                "//OBJECT_TYPE[text() = '" + type.getObjectType()
                        + "']/following-sibling::param/[position()='" + x
                        + "']/'" + columnName + "'").evaluate(xmlDocument,
                XPathConstants.NODESET);

        // Set new NodeValue
        node.setNodeValue(data.toString());

        // Save the new updates
        try {
            save(file, xmlDocument);
        } catch (Exception e) {

            e.printStackTrace();
        }

    }

// Method for saving the new updated data.
    public void save(File file, Document doc) throws Exception {

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        // Holds the old document
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String s = writer.toString();
        System.out.println(s);

        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        bufferedWriter.write(s);
        bufferedWriter.flush();
        bufferedWriter.close();
    }

how to fix this?

Update :

This code solves the problem but the node is null so the expression is working.

Node node = (Node) xPath.compile(
                "//OBJECT_TYPE[text() = '" + type.getObjectType()
                        + "']/following-sibling::param[position()= '" + x
                        + "']/" + columnName + "").evaluate(xmlDocument,
                XPathConstants.NODE);
3
  • When the exception occurs, what does the XPath expression look like? Commented Jan 30, 2014 at 11:10
  • the exception occurs as soon as I try to edit the data ain my cell. The path expression is ther ein my code. So the line the exceptoin is refering to is the line the I compile the xpression. Commented Jan 30, 2014 at 11:21
  • No, the code you use to build the expression is there in your code. The expression is not. The expression consists of a combination of those strings and the values of some variables and method calls. Those values are not present in the question, so we can't see what your XPath expression looks like. Commented Jan 30, 2014 at 11:30

1 Answer 1

1

So far, I see two problems:

param*

A name test in XPath doesn't work the same way as a glob. Broadly speaking, you either match a name completely, or you use * to match any node of the principal node type for that axis (normally an element).

As such, param* does not match things like param, param0, param-something-else, it's just an invalid expression.

/[

You can't have a predicate filtering nothing. A nodeset must be matched by a nodetest first, then filtered by a predicate.

There are two possibilities here:

  1. The / is superfluous and must be removed (if you are trying to use the predicate to filter the nodeset returned by the expression preceding the /).

  2. A nodetest must be inserted after the /, (if you are trying to filter the children or descendants of nodes within that nodeset).

Sign up to request clarification or add additional context in comments.

4 Comments

its telling me "A location step is needed after token '/' eller '//'.
Right, so either place a location step where one is missing, or remove the / or // where the location step is missing.
yes it should be no "/" before the position it was. But now it complains at this token ''PARAMETER'' this comesfrom my columnName variable : /'" + columnName + "'. hm..
aight now its done. hm my new expression is giving me nullpointer exception. The node is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.