2016-11-21 19 views
0

我有一個xml:看我的XML與JAXB

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<measures> 
     <measure isapplicationaggregated="true" errorseverity="none" servicecontext="SERVER" userdefined="false" id=".NET % Time in Jit" calculatepercentiles="false" createdtimestamp="1478709202942" errortype="none" rate="purepath" description="Percentage of elapsed time spent in JIT compilation since the last JIT compilation phase." ischartable="true" metricid=".NET % Time in Jit" measuretype="PerfMonMeasure" isaggregated="false" metricgroupid=".NET Common Language Runtime" displayaggregations="7" calculatebaseline="false" displayunit="percent"> 
     <perfmonperformancecounter performanceobject=".NET CLR Jit" instance="{monitored_process}" performancecounter="% Time in Jit" /> 
     <color color.blue="64" color.green="0" color.red="64" /> 
     </measure> 
     <measure isapplicationaggregated="true" errorseverity="none" servicecontext="SERVER" userdefined="false" id=".NET Garbage Collection (# Gen 0)" calculatepercentiles="false" createdtimestamp="1478709202942" errortype="none" rate="purepath" description="Number of times the generation 0 objects were garbage collected (Gen 0 GC) per interval." ischartable="true" metricid=".NET Garbage Collection (# Gen 0)" measuretype="PerfMonMeasure" isaggregated="false" metricgroupid=".NET Common Language Runtime" displayaggregations="31" calculatebaseline="false" displayunit="number"> 
     <perfmonperformancecounter performanceobject=".NET CLR Memory" instance="{monitored_process}" performancecounter="# Gen 0 Collections" /> 
     <color color.blue="64" color.green="192" color.red="128" /> 
     </measure> 
</measures> 

我需要使用JAXB讀者除了口口聲聲說不能讀它翻譯。

我只需要measure對象的userdefined,idmeasuretype屬性。

下面的代碼是我到目前爲止有:

XML

@XmlRootElement(name = "measures") 
public class MeasureListWrapper { 

     private List<Property> measureProperties = new ArrayList<Property>(); 

     @XmlElement(name="measure") 
     public List<Property> getMeasures() { 
      return measureProperties; 
     } 
} 

MainApp:

JAXBContext context = JAXBContext.newInstance(MeasureListWrapper.class); 
      Unmarshaller um = context.createUnmarshaller(); 

      // Reading XML from the file and unmarshalling. 
      MeasureListWrapper wrapper = (MeasureListWrapper) um.unmarshal(file); 

      Marshaller marshaller = context.createMarshaller(); 
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
      marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "file:///C:/Users/mydesk/Desktop/FirstXSD.xml"); 
      marshaller.marshal(wrapper, System.out); 

我是新來JAXB並不能得到這個工作。真的,我只需要將這些xml中的首選屬性導出到文本文件。

我還想存儲ID和度量類型以顯示在我的小程序窗口中的表中。

任何幫助?

謝謝!

回答

1

這爲我工作:

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

public class MeasureParser { 

    @XmlRootElement(name = "measure") 
    public static class Measure { 
     @XmlAttribute(name = "id") 
     private String id; 

     @XmlAttribute(name = "measuretype") 
     private String measureType; 

     @XmlAttribute(name = "userdefined") 
     private boolean userDefined; 
    } 

    @XmlRootElement(name = "measures") 
    public static class MeasureListWrapper { 

     private List<Measure> measureProperties = new ArrayList<>(); 

     @XmlElement(name = "measure") 
     public List<Measure> getMeasures() { 
      return measureProperties; 
     } 
    } 

    public static void main(String[] args) throws JAXBException { 
     JAXBContext context = JAXBContext.newInstance(MeasureListWrapper.class); 
     Unmarshaller um = context.createUnmarshaller(); 

     // Reading XML from the file and unmarshalling. 
     MeasureListWrapper wrapper = (MeasureListWrapper) um.unmarshal(new File("test.xml")); 

     Marshaller marshaller = context.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "file:///C:/Users/mydesk/Desktop/FirstXSD.xml"); 
     marshaller.marshal(wrapper, System.out); 
    } 
}