从没有根节点的文件中提取后,将根节点添加到 XML
我有记录方法调用的代码。这样它就可以通过在末尾附加来进行记录,我让它在没有根节点的情况下进行记录...所以结构是这样的...
<method>
<handle id="0"/>
<timestamp>0</timestamp>
<signature>int[] path.getSelectedObjectIds()</signature>
<arguements>
<argument>rO0ABXA=</argument>
</arguements>
<return>value</return>
</method>
<method>
<handle id="0"/>
<timestamp>0</timestamp>
<signature>int path.getObjectIDFromKey(String)</signature>
<arguements>
<argument>Selectable1</argument>
<argument>2</argument>
</arguements>
<return>value</return>
</method>
所以现在我正在尝试读取这个文件以使用类似的内容进行播放这
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("methodLog.txt"));
// normalize text representation
doc.getDocumentElement().normalize();
NodeList listOfMethods = doc.getElementsByTagName("method");
int totalMethods = listOfMethods.getLength();
System.out.println("Number of methods: " + totalMethods);
for (int i = 0; i < listOfMethods.getLength(); i++) {
...
并得到一个错误,该文档必须是格式良好的。有没有一种快速方法可以立即在文件中当前存在的内容周围添加根标记(例如“方法”)?
I have code which logs method calls. So that it is able to log by just appending at the end, I am having it log without a root node... so the structure is like this...
<method>
<handle id="0"/>
<timestamp>0</timestamp>
<signature>int[] path.getSelectedObjectIds()</signature>
<arguements>
<argument>rO0ABXA=</argument>
</arguements>
<return>value</return>
</method>
<method>
<handle id="0"/>
<timestamp>0</timestamp>
<signature>int path.getObjectIDFromKey(String)</signature>
<arguements>
<argument>Selectable1</argument>
<argument>2</argument>
</arguements>
<return>value</return>
</method>
so now i'm trying to read this file for playback with something like this
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("methodLog.txt"));
// normalize text representation
doc.getDocumentElement().normalize();
NodeList listOfMethods = doc.getElementsByTagName("method");
int totalMethods = listOfMethods.getLength();
System.out.println("Number of methods: " + totalMethods);
for (int i = 0; i < listOfMethods.getLength(); i++) {
...
and getting an error that the document must be well-formed. is there a quick way to throw a root tag (say "methods") around what currently exists in the file right away?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在使用 htmltext 到 xml 时遇到了同样的问题。 我将
其解析为字符串并添加了标签:
所以新 xml 的输出是:
这是使其工作的简单方法。希望这可以帮助你。
I had the same problem by using htmltext into xml. It was
I parsed it into string and just added the tags:
So the output of the new xml was:
It a simple way to make it work. Hope this could help you.