从没有根节点的文件中提取后,将根节点添加到 XML

发布于 2024-11-06 16:51:47 字数 1441 浏览 2 评论 0原文

我有记录方法调用的代码。这样它就可以通过在末尾附加来进行记录,我让它在没有根节点的情况下进行记录...所以结构是这样的...

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-11-13 16:51:47

我在使用 htmltext 到 xml 时遇到了同样的问题。 我将

<p align="left"><font..>text</font></p><p align="left"><font ..>text</font></p>

其解析为字符串并添加了标签:

var s:String = xmlfile.toXmlString(); //don't know what it is in your language I use AS3
var newString:String = "<html>" + s + "</html>"; //adding the tags
xmlfile = new XML(newString); //parse new string into xml

所以新 xml 的输出是:

<html>
     <p align="left">
          <font..>text</font>
     </p>
     <p align="left">
          <font ..>text</font>
     </p>
</html>

这是使其工作的简单方法。希望这可以帮助你。

I had the same problem by using htmltext into xml. It was

<p align="left"><font..>text</font></p><p align="left"><font ..>text</font></p>

I parsed it into string and just added the tags:

var s:String = xmlfile.toXmlString(); //don't know what it is in your language I use AS3
var newString:String = "<html>" + s + "</html>"; //adding the tags
xmlfile = new XML(newString); //parse new string into xml

So the output of the new xml was:

<html>
     <p align="left">
          <font..>text</font>
     </p>
     <p align="left">
          <font ..>text</font>
     </p>
</html>

It a simple way to make it work. Hope this could help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文