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