xml注解驱动开发

对于xml文件,常规方法通过dom4j和一些api方法来进行解析,但是这种方法需要自己解析xml文件,为了方便解析xml和效率,jdk引入jakarta.xml.bind.annotation

相关注解来提升xml解析效率

假定有当前xml文件

<fixing name="" displayName="CL03" id="fixing1" partnumber="" length="0.008" fixingType="Clip">
    <property name="Component Type" val="Clip-CLA" />
    <property name="CHSFixingType" val="Clip" />
    <property name="CHSFixingLength" val="0.008" />
    <Location id="fixing1.Location1" bundleRef="bundle1">
        <bundleLocation offsetparameter="0" offsetnoderef="start" />
    </Location>
    <Location id="fixing1.Location2" bundleRef="bundle61">
        <bundleLocation offsetparameter="1" offsetnoderef="start" />
    </Location>
    <fixingPlane>
        <origin x="0.559401" y="-0.634963" z="0.483466" />
        <Normal x="-1.000000" y="-0.000000" z="0.000000" />
    </fixingPlane>
    <fixingAxis>
        <Direction x="-1.000000" y="-0.000000" z="0.000000" />
    </fixingAxis>
    <fixingPoint>
        <Position x="0.559401" y="-0.637056" z="0.491240" />
    </fixingPoint>
    <inPlane>
        <origin x="0.544363" y="-0.637063" z="0.495252" />
        <Normal x="-0.000000" y="-0.038528" z="-0.999258" />
    </inPlane>
    <outPlane>
        <origin x="0.544363" y="-0.637371" z="0.487258" />
        <Normal x="-0.000000" y="-0.038528" z="-0.999258" />
    </outPlane>
    <Tangentdiretion>
        <Direction x="-0.000000" y="-0.038528" z="-0.999258" />
    </Tangentdiretion>
</fixing>

使用注解解析

依赖

<!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>4.0.0</version>
</dependency>

@XmlAccessorType(XmlAccessType.FIELD)//此注解是只能解析protect类型的属性
@XmlType(
        name = "FixingType",//解析成java类的名称
        propOrder = {"propertyGroup", "location", "addpart", "fixingPoint", "inPlane", "outPlane"}//定义解析xml元素的顺序
)
public class FixingType {
	//propertyGroup集合对象是由这些元素组成
    @XmlElements({@XmlElement(
            name = "property",
            type = BasePropertyType.class
    ), @XmlElement(
            name = "variant",
            type = BaseVarianceType.class
    ), @XmlElement(
            name = "functionalvariant",
            type = FunctionalVariantType.class
    ), @XmlElement(
            name = "productionvariant",
            type = ProductionVariantType.class
    ), @XmlElement(
            name = "refedmodule",
            type = BaseReferredModuleType.class
    )})
    protected List<Object> propertyGroup;
    @XmlElement(name = "Location")//解析<Location>标签
    protected List<BundleLocationType> location;
    protected List<AdditionalComponentType> addpart;
    @XmlElement(name = "fixingPoint", type = FixingPoint.class)//解析<fixingPoint>标签
    protected FixingPoint fixingPoint;
	//解析<inPlane>标签
    @XmlElement(name = "inPlane", type = InPlane.class)
    protected InPlane inPlane;
	//解析<outPlane>标签
    @XmlElement(name = "outPlane", type = OutPlane.class)
    protected OutPlane outPlane;
	//解析FixingType类型里面的属性
    @XmlAttribute(
            name = "id",
            required = true
    )
    protected String id;
    @XmlAttribute(
            name = "elecid"
    )
    protected String elecid;
    @XmlAttribute(
            name = "length"
    )
    protected Double length;
    @XmlAttribute(
            name = "fixingType"
    )
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String fixingType;
    @XmlAttribute(
            name = "orientation"
    )
    protected String orientation;
    @XmlAttribute(
            name = "optionexpression"
    )
    protected String optionexpression;
    @XmlAttribute(
            name = "materialcode"
    )
    protected String materialcode;
    @XmlAttribute(
            name = "colorcode"
    )
    protected String colorcode;
    @XmlAttribute(
            name = "group"
    )
    protected String group;
    @XmlAttribute(
            name = "viewFromNode"
    )
    @XmlIDREF
    @XmlSchemaType(
            name = "IDREF"
    )
    protected Object viewFromNode;
    @XmlAttribute(
            name = "cliptype"
    )
    protected ClipType cliptype;
    @XmlAttribute(
            name = "clipmount"
    )
    protected ClipMountingType clipmount;
    @XmlAttribute(
            name = "offsetclip"
    )
    protected Boolean offsetclip;
    @XmlAttribute(
            name = "supplierpart"
    )
    @XmlSchemaType(
            name = "anySimpleType"
    )
    protected String supplierpart;
    @XmlAttribute(
            name = "customerpart"
    )
    @XmlSchemaType(
            name = "anySimpleType"
    )
    protected String customerpart;
    @XmlAttribute(
            name = "partnumber"
    )
    protected String partnumber;
    @XmlAttribute(
            name = "partrevision"
    )
    protected String partrevision;
    @XmlAttribute(
            name = "incBom"
    )
    protected Boolean incBom;
    @XmlAttribute(
            name = "librarytypecode"
    )
    protected String librarytypecode;
    @XmlAttribute(
            name = "shortdescription"
    )
    protected String shortdescription;
    @XmlAttribute(
            name = "name"
    )
    protected String name;
    @XmlAttribute(
            name = "displayName"
    )
    protected String displayName;
    @XmlAttribute(
            name = "architecturalCost"
    )
    protected Double architecturalCost;
    @XmlAttribute(
            name = "weight"
    )
    protected Double weight;

    public FixingType() {
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
        name = "InPlane",
        propOrder = {"origin"}
)
public class InPlane {
    @XmlElement(
            name = "origin"
    )
    protected Origin origin;

    public Origin getOrigin() {
        return origin;
    }

    public void setOrigin(Origin origin) {
        this.origin = origin;
    }
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
        name = "Origin"
)
public class Origin {
    @XmlAttribute(
            name = "x",
            required = true
    )
    protected double x;
    @XmlAttribute(
            name = "y",
            required = true
    )
    protected double y;
    @XmlAttribute(
            name = "z"
    )
    protected Double z;

    public Origin() {
    }

    public double getX() {
        return this.x;
    }

    public void setX(double value) {
        this.x = value;
    }

    public double getY() {
        return this.y;
    }

    public void setY(double value) {
        this.y = value;
    }

    public Double getZ() {
        return this.z;
    }

    public void setZ(Double value) {
        this.z = value;
    }
}

生成javabean

File file = new File("src/main/resources/demo.xml");
        // create JAXB context and unmarshaller
        JAXBContext context = JAXBContext.newInstance(Demo.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // unmarshal XML to object
        Demo demo = (Demo) unmarshaller.unmarshal(file);
        System.out.println(demo);

老方法dom4j

定义xml文件

<?xml version="1.0"?>
<root>
	<point y="0" x="0" name="A" />
	<point y="200" x="100" name="B"/>
	<point y="-50" x="300" name="C"/>
	<point y="-100" x="100" name="D"/>
	<point y="100" x="-100" name="E"/>
	<path startpoint="A" endpoint="B" name="p1"/>
	<path startpoint="A" endpoint="C" name="p2"/>
	<path startpoint="B" endpoint="C" name="p3"/>
	<path startpoint="B" endpoint="D" name="p4"/>
	<path startpoint="C" endpoint="B" name="p5"/>
	<path startpoint="C" endpoint="D" name="p6"/>
	<path startpoint="E" endpoint="C" name="p7"/>
	<path startpoint="C" endpoint="E" name="p8"/>
	<path startpoint="E" endpoint="A" name="p9"/>
	<path startpoint="A" endpoint="E" name="p10"/>
</root>
@Override
public Map anaylizeXml(MultipartFile file) {
    HashMap<String, Object> map = new HashMap<>();
    SAXReader reader = new SAXReader();
    try {
        //从resource下获取文件信息
//            URL resource  = XmlAnaylizeApplication.class.getClass().getResource("/xml/demo.xml");
//            String path = resource.getPath();
        //从前端获取文件信息
        Document document = reader.read(file.getInputStream());
        //获取root
        Element rootElement = document.getRootElement();
        Iterator iterators = rootElement.elementIterator();
        pointList = new ArrayList<Point>();
        pathList = new ArrayList<Path>();
        while (iterators.hasNext()) {
            point = new Point();
            path = new Path();
            Element element = (Element) iterators.next();
            //获取point标签
            if (element.getName().equals("point")) {
                List<Attribute> attributes = element.attributes();
                for (Attribute attribute : attributes) {
                    if (attribute.getName().equals("y")) {
                        String y = attribute.getValue();
                        point.setY(Integer.parseInt(y));
                    }
                    if (attribute.getName().equals("x")) {
                        String x = attribute.getValue();
                        point.setX(Integer.parseInt(x));
                    }
                    if (attribute.getName().equals("name")) {
                        String name = attribute.getValue();
                        point.setPointName(name);
                    }
                }
                pointList.add(point);
            }
            //path
            if (element.getName().equals("path")) {
                List<Attribute> attributes = element.attributes();
                for (Attribute attribute : attributes) {
                    if (attribute.getName().equals("startpoint")) {
                        String startpoint = attribute.getValue();
                        path.setStartPoint(startpoint);
                    }
                    if (attribute.getName().equals("endpoint")) {
                        String endpoint = attribute.getValue();
                        path.setEndPoint(endpoint);
                    }
                    if (attribute.getName().equals("name")) {
                        String name = attribute.getValue();
                        path.setPathName(name);
                    }
                }
                pathList.add(path);
            }
        }
        map.put("points", pointList);
        map.put("paths", pathList);
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return map;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值