gpt4 book ai didi

尝试更新 XML 中的值时出现 javax.xml.xpath.XPathExpressionException

转载 作者:行者123 更新时间:2023-12-01 13:32:56 26 4
gpt4 key购买 nike

我有一个具有如下 xml 的应用程序:

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

等等

所有这些数据都是从 XML 解析的,现在用户应该能够编辑数据。所以我实现了一种更新参数的方法:

但由于错误地编写了 Xpath 语句,它给了我一个异常(exception):

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

所以我尝试做的是从用户编辑的表中获取rowCount并将其存储在int x中。然后我将 columnName 存储在 String columnName 中。我尝试在 xpath 表达式中使用此变量来根据用户选择的 ObjectType 找到完美的位置。并更新并保存在xml文件中。但不知何故,我的 xpath 表达式似乎是错误的,或者变压器无法以某种方式读取它。因为我也得到了这个:

Caused by: javax.xml.transform.TransformerException

这是我的java代码:

// 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();
}

如何解决这个问题?

更新:

此代码解决了问题,但节点为空,因此表达式可以正常工作。

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

最佳答案

到目前为止,我发现两个问题:

参数*

一个name test XPath 中的工作方式与 glob 不同。一般来说,您要么完全匹配名称,要么使用 * 来匹配该轴的主要节点类型的任何节点(通常是一个元素)。

因此,param*paramparam0param-something-else 等内容不匹配,这只是一个无效的表达式。

/[

你不能让谓词不过滤任何内容。节点集必须与 nodetest 匹配首先,然后通过谓词进行过滤。

这里有两种可能性:

  1. / 是多余的,必须删除(如果您尝试使用谓词来过滤 / 之前的表达式返回的节点集)。

  2. 必须在 / 之后插入节点测试(如果您尝试过滤该节点集中节点的子节点或后代)。

关于尝试更新 XML 中的值时出现 javax.xml.xpath.XPathExpressionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21453840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com