2016-04-27 28 views
0

私はこのようなXMLを含む文字列を持っている:変換の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> 

を私は各インデントのための私のツリービューでのTreeNodeを必要とするとき、私、私はそれを拡張し、契約することができますので、各ノードには非常に多くの情報があるので、どうしたらいいですか?

結果は次のようにする必要があります:

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

ありがとうございました!

答えて

2

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

あなたは素晴らしいです!それは魅力のように働き、私に多くの時間を救った!!どうもありがとうございました!! – master89

+0

ツリービューに表示するアイテムの数を制限するにはどうすればよいですか? – master89

+0

正確に何を意味するのかわからない... N個のアイテムだけを表示したい場合は、ルートアイテムの 'N-1'個のデセンダントがあることを確認するか、自動的に一部を折りたたむリスナを使うことができますあまりにも多くの項目が展開されている場合は... – fabian