我有这个 xml,我需要在其中获取 CPU 信息,但CPU"1"
数字部分的标签可能会改变。我怎样才能给出一个 xpath 以便它像直到一样动态地描绘数字部分n
?
我的 XML 内容
<CLIENT>
<SYSTEM>
<CPU1 NUMBER="2" SPEED="2300 MHz" Vendor="Advanced Micro Devices" BRAND="AMD Opteron(tm) Processor 6134"></CPU1>
<CPU2 NUMBER="3" SPEED="2300 MHz" Vendor="Advanced Micro Devices" BRAND="AMD Opteron(tm) Processor 6134"></CPU2>
</SYSTEM>
</CLIENT>
目前我得到类似下面的xml从给定的代码生成的
<client>
<system>
<cpuinfo>
<cpu Name="AMD Opteron(tm) Processor 6134" L2CacheSize="NA" MaxClockSpeed="2300 MHz" LoadPercentage="NA"/>
</cpuinfo>
</system>
</client>
我的代码
@XStreamAlias("cpu")
class cpu{
@XStreamAsAttribute
String Name
@XStreamAsAttribute
String L2CacheSize
@XStreamAsAttribute
String MaxClockSpeed
@XStreamAsAttribute
String LoadPercentage
public cpu(String name, String l2CacheSize, String maxClockSpeed,String loadPercentage) {
super();
Name = name;
L2CacheSize = l2CacheSize;
MaxClockSpeed = maxClockSpeed;
LoadPercentage = loadPercentage;
}
}
@XStreamAlias("system")
public class WindowsSystem extends CSMSystem{
List<cpu> cpuinfo
public WindowsSystem(CSMConfig config, String fileContent){
super(config)
cpuinfo = new ArrayList<cpu>()
WindowsHelper wh=new WindowsHelper(fileContent)
cpuinfo=wh.getCpuInfo(cpuinfo)
}
}
class WindowsHelper {
private def root
private List<cpu> cpuinfo
WindowsHelper(String fileContents)
{
root=new XmlParser(false,false).parseText(fileContents)
}
def getCpuInfo(List<cpu> cpuinfo)
{
try{
def cpusize="${root.SYSTEM.CPU1.size()}"
if(cpusize>0){
root.SYSTEM.CPU1.each{
cpuinfo.add(new cpu(it.@BRAND,"NA",it.@SPEED,"NA"))
}
return cpuinfo
}
else{
cpuinfo.add(new cpu("NA","NA","NA","NA"))
return cpuinfo
}
}
catch(Exception e){
println "getCpuInfo "+e
}
}
}