当前位置: 首页 > 知识库问答 >
问题:

在JavaFX中将xml字符串转换为带节点的treeview

锺离声
2023-03-14

我有一个包含如下xml的字符串:

<root>
   <type>
      <element>
         <thing>
            <otherthing>...</otherthing>
         </thing>
      </element>
   </type>
   <type>
      <element>
         <thing>
            <otherthing>...</otherthing>
         </thing>
      </element>
   </type>
   <type>
      <element>
         <thing>
            <otherthing>...</otherthing>
         </thing>
      </element>
   </type>
</root>

我需要在我的树视图中为每个缩进创建一个树节点,这样我可以在需要的时候扩展和收缩它,因为每个节点中都有这么多信息,我该怎么做?

结果应该是这样的:

ROOT
---+type
--------+element
----------------+thing
----------------------+otherthing
---+type
--------+element
----------------+thing
----------------------+otherthing
---+type
--------+element
----------------+thing
----------------------+otherthing

非常感谢。

共有1个答案

楚浩然
2023-03-14

使用xml解析器解析数据,创建表示数据的TreeItem,并使用TreeView显示数据。

例子:

private static class TreeItemCreationContentHandler extends DefaultHandler {

    private TreeItem<String> item = new TreeItem<>();

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        // finish this node by going back to the parent
        this.item = this.item.getParent();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        // start a new node and use it as the current item
        TreeItem<String> item = new TreeItem<>(qName);
        this.item.getChildren().add(item);
        this.item = item;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String s = String.valueOf(ch, start, length).trim();
        if (!s.isEmpty()) {
            // add text content as new child
            this.item.getChildren().add(new TreeItem<>(s));
        }
    }

}

public static TreeItem<String> readData(File file) throws SAXException, ParserConfigurationException, IOException {
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    SAXParser parser = parserFactory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    TreeItemCreationContentHandler contentHandler = new TreeItemCreationContentHandler();

    // parse file using the content handler to create a TreeItem representation
    reader.setContentHandler(contentHandler);
    reader.parse(file.toURI().toString());

    // use first child as root (the TreeItem initially created does not contain data from the file)
    TreeItem<String> item = contentHandler.item.getChildren().get(0);
    contentHandler.item.getChildren().clear();
    return item;
}
// display data for file "data/tree.xml" in TreeView
TreeItem<String> root = readData(new File("data/tree.xml"));
TreeView<String> treeView = new TreeView<>(root);
 类似资料:
  • 问题内容: 您好,我正在从Web服务获取一个字符串。 我需要解析此字符串并获取错误消息中的文本? 我的字符串如下所示: 仅分析字符串还是将其转换为xml然后进行分析是否更好? 问题答案: 我将使用Java的XML文档库。有点混乱,但是可以。

  • 问题内容: 我是Go的新手,正在尝试执行以下操作: 我搜索了很多,但真的不知道该怎么做。 我知道这行不通: 问题答案: 这不是实现它的最有效方法,但是您可以简单地编写: 被称为:

  • 问题内容: 我想在GO中将字符串数组转换为字节数组,以便可以将其写到磁盘上。将字符串数组()解码为字节数组()的最佳解决方案是什么? 我正在考虑对字符串数组进行两次迭代,第一个迭代以获得字节数组所需的实际大小,然后第二个迭代写入每个元素的长度和实际字符串()。 解决方案必须能够以其他方式进行转换;从一个到一个。 问题答案: 让我们忽略一个事实,那就是走一秒钟。您需要做的第一件事是将序列化格式编组为

  • 最近我一直在使用Altova Map Force构建XSL转换,我遇到了一个问题。为了创建一个有效的XML输出文件(从一个输入文本文件创建,该文件非常简单),它需要包含一些xmlns属性标记。 不幸的是,我无法在Map Force或我的数据转换工具(由基于Saxon的客户机构建)中找到有效的方法(修改了输出xsd模式定义和属性强制)。因此,我的解决方案是将生成的XML解析到另一个(最终)转换器中,

  • 问题内容: 我正在使用以下代码从外部程序获取标准输出: 方法返回一个字节数组: 但是,我想将输出作为普通的字符串使用。这样我就可以像这样打印它: 我认为这就是方法的用途,但是当我尝试使用它时,我又得到了相同的字节数组: 如何将字节值转换回字符串?我的意思是,使用”batteries”而不是手动进行操作。我希望它与Python 3兼容。 问题答案: 你需要解码bytes对象以产生一个字符串:

  • 问题内容: 我有以下代码,我试图通过测试,但似乎无法理解Java世界中各种编码形式。 我想我的问题是:将任意字节的字节数组转换为Java字符串,然后将同一Java String转换为另一个字节数组的正确方法是什么,该字节数组将具有与原始字节相同的长度和相同的内容数组? 问题答案: 尝试特定的编码: ideone链接