【问题标题】:XML - Understanding difficulties parsing XML using JDOMXML - 理解使用 JDOM 解析 XML 的困难
【发布时间】:2013-08-16 10:56:02
【问题描述】:

我得到了这个特殊的 XML 文件,它看起来像这样:

<App01_Storage Type="VOLATILE" Size="200000" Speed="15" LatencyMaxWrite="1" LatencyMaxRead="2" Endurance="12" WorkloadPeak="15" />

在我的程序中,我遍历根节点的所有子节点。我的目的是让所有孩子都有他们的属性+价值观。一个孩子看起来像上面的代码。

System.out.println(node.getName());
System.out.println(node.getAttributes());

System.Out-Method 给了我这个输出: App01_Storage [[属性:Type="VOLATILE"],[属性:Size="200000"],[属性:Speed="15"],[属性:LatencyMaxWrite="1"],[属性:LatencyMaxRead="2"] , [属性: Endurance="12"], [属性: WorkloadPeak="15"]]

我想我走对了。根据我的理解,一个属性应该是这样的:Attribute.Name = Attribute.Value

我想将属性和值保存在不同的类中,但不知道如何准确地获取值和名称。我现在得到的输出是一个列表,其中每个条目 Attributename = Attributevalue,就像一个字符串。使用这个单一的字符串我无法工作。

我看错了吗?希望我能解释一下自己。非常感谢:)

【问题讨论】:

    标签: xml xml-parsing jdom


    【解决方案1】:

    node.getAttributes() 为您提供了一个属性列表,您可以再次对其进行迭代。您看到的输出只是列表toString() 方法的结果,当您将其交给System.out.println() 时会调用该方法。

    做这样的事情:

    for(Attribute attribute : node.getAttributes()) {
        String attributeName = attribute.getName();
        String value = attribute.getValue();
    }
    

    还有其他 get-methods 已经返回某种类型(如getIntValue())。看Attribute documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多