使用JAXB读取我的XML

时间:2016-11-21 21:56:06

标签: java xml jaxb

我有一个xml:

<?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阅读器翻译它,除非它一直说它无法读取它。

我只需要userdefined对象中的idmeasuretypemeasure属性。

以下代码是我目前的代码:

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和measuretypes存储在applet窗口的表格中。

任何帮助?

谢谢!

1 个答案:

答案 0 :(得分: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);
    }
}