Ignacious
Ignacious

Reputation: 119

Reading my XML with JAXB

I have an 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>

I need to translate it using the JAXB reader except it keeps saying it cannot read it.

I only need the userdefined , id , and measuretype properties from the measure object.

The code below is what I have so far:

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

I am new to JAXB and can't get this to work. Really I just need to export the preferred properties to a text file after I pull them from this xml.

I would also like to store the ID's and measuretypes to display in a table in my applet window.

Any help?

Thanks!

Upvotes: 0

Views: 131

Answers (1)

teppic
teppic

Reputation: 7286

This worked for me:

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

Upvotes: 1

Related Questions