XML节点列表作为JavaFX可观察列表?

XML节点列表作为JavaFX可观察列表?,java,xml,parsing,javafx,observablelist,Java,Xml,Parsing,Javafx,Observablelist,有问题 我有一些包含根元素的XML文件,3个元素具有相同的名称和不同的属性,而且每个元素都有一些元素 我希望在FX可观察列表中有名为“id”的元素属性。 不知道该怎么办。因为你没有给我太多信息,我不得不用一些伪代码来解释 假设您有以下XML: <root id="0"> <sub1 id="1"> <hell1>hell1</hell1> </sub1> <sub2>

有问题

我有一些包含根元素的XML文件,3个元素具有相同的名称和不同的属性,而且每个元素都有一些元素

我希望在FX可观察列表中有名为“id”的元素属性。
不知道该怎么办。

因为你没有给我太多信息,我不得不用一些伪代码来解释
假设您有以下XML:

<root id="0">
    <sub1 id="1">
        <hell1>hell1</hell1>
    </sub1>
    <sub2>
        <hell2 id="2">hell2</hell2>
    </sub2>
    <sub3>sub3</sub3>
</root>
如果您不知道如何在Java中使用XML和Xpath,可以在此处查找:



如果我想从XML中获取所有“id”属性值,它将如下所示:

//我的测试XML
最终字符串xml=“\n”+
“\n”+
“\t\n”+
“\t\tl1\n”+
“\t\n”+
“\t\n”+
“\t\tl2\n”+
“\t\n”+
“\tsub3\n”+
"";
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
//这里您必须放置XML源(字符串、输入流、文件)
Document doc=builder.parse(新的InputSource(新的ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathFactory.newXPath();
//上面的xpath
XPathExpression expr=xpath.compile(“//*[@id]”);
//在这里,您可以获得名为“id”的属性的所有元素的节点列表
NodeList NodeList=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
for(int i=0;i
我假设您知道如何将这些值放入可观察列表中。:)
如果您还有任何问题,请随时提问


PS:如果你给我看你的XML,我可以更有效地帮助你。

因为你没有给我太多信息,我不得不用一些伪代码来解释它
假设您有以下XML:

<root id="0">
    <sub1 id="1">
        <hell1>hell1</hell1>
    </sub1>
    <sub2>
        <hell2 id="2">hell2</hell2>
    </sub2>
    <sub3>sub3</sub3>
</root>
如果您不知道如何在Java中使用XML和Xpath,可以在此处查找:



如果我想从XML中获取所有“id”属性值,它将如下所示:

//我的测试XML
最终字符串xml=“\n”+
“\n”+
“\t\n”+
“\t\tl1\n”+
“\t\n”+
“\t\n”+
“\t\tl2\n”+
“\t\n”+
“\tsub3\n”+
"";
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
//这里您必须放置XML源(字符串、输入流、文件)
Document doc=builder.parse(新的InputSource(新的ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathFactory.newXPath();
//上面的xpath
XPathExpression expr=xpath.compile(“//*[@id]”);
//在这里,您可以获得名为“id”的属性的所有元素的节点列表
NodeList NodeList=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
for(int i=0;i
我假设您知道如何将这些值放入可观察列表中。:)
如果您还有任何问题,请随时提问


PS:如果您向我展示您的XML,我可以更有效地帮助您。

了解从XML中选择不同节点的X-Path技术。了解从XML中选择不同节点的X-Path技术。
// My Test-XML
final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
        "<root id=\"0\">\n" +
        "\t<sub1 id=\"1\">\n" +
        "\t\t<hell1>hell1</hell1>\n" +
        "\t</sub1>\n" +
        "\t<sub2>\n" +
        "\t\t<hell2 id=\"2\">hell2</hell2>\n" +
        "\t</sub2>\n" +
        "\t<sub3>sub3</sub3>\n" +
        "</root>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// Here you have to put your XML-Source (String, InputStream, File)
        Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
// The xpath from above
XPathExpression expr = xpath.compile("//*[@id]");
// Here you get the node-list of all elements with an attribute named "id"
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
    // run through all nodes
    Node node = nodeList.item(i);
    // fetch the values from the "id"-attribute
    final String idValue = node.getAttributes().getNamedItem("id").getNodeValue();
    // Do something awesome!!! 
    System.out.println(idValue);
}