繁体   English   中英

用JAXB读取我的XML

[英]Reading my XML with 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阅读器来翻译它,但它一直说无法阅读。

我只需要userdefinedidmeasuretype从性能measure对象。

到目前为止,我下面的代码是:

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和度量类型以显示在applet窗口的表中。

有什么帮助吗?

谢谢!

这为我工作:

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM