How to generate xml file with an existing dtd for sample of data using Java

旧城冷巷雨未停 提交于 2021-02-11 12:29:36

问题


I have some data stored in :

HashMap<Position, Double> listOfPoints = new HashMap<>();

Where key contains pair (x,y).

I have defined also my dtd file called generated.dtd which contains my structure :

<!ELEMENT table (point*,id,position,x,y,value)>
<!ELEMENT point (id,position,value)>
<!ELEMENT id (ID)>
<!ELEMENT position (x,y)>
<!ELEMENT value (#PCDATA)>
<!ELEMENT x (#PCDATA)>
<!ELEMENT y (#PCDATA)>

Using that file and sample of data stored in listOfPoints , I would like to generate an xml file that corresponds to my generated.dtd structure.

How could i do that in Java?

If you have a tutorial to follow , it will be great .

Iknow i should use JAXB : Java Architecture for XML Binding, but i don't know how ?


回答1:


A couple of observations/suggestions:

1) What do you need the XML output to look like?

Assume you have a Points class like this:

import java.util.Map;
import java.util.HashMap;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "root")
public class Points {

    public Points() {};

    public Points(Map<Position, Double> listOfPoints) {
        this.listOfPoints = listOfPoints;
    }

    @XmlElement(name = "list_of_points")
    private Map<Position, Double> listOfPoints;

    public Map<Position, Double> getListOfPoints() {
        if (listOfPoints == null) {
            listOfPoints = new HashMap();
        }
        return this.listOfPoints;
    }
}

and a Position class like this:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "position")
public class Position {

    @XmlElement(required = true)
    protected String x;
    @XmlElement(required = true)
    protected String y;

    public String getX() {
        return x;
    }

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

    public String getY() {
        return y;
    }

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

}

Without using a DTD, you can generate XML like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <listOfPoints>
        <entry>
            <key>
                <x>pos1 x</x>
                <y>pos1 y</y>
            </key>
            <value>123.456</value>
        </entry>
        <entry>
            <key>
                <x>pos2 x</x>
                <y>pos2 y</y>
            </key>
            <value>456.789</value>
        </entry>
    </listOfPoints>
</root>

The code to do that is:

JAXBContext jc = JAXBContext.newInstance(Points.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
File xml = new File("/path/to/points.xml");
marshaller.marshal(points, xml);

Is that sufficient for your needs?

2) Using your DTD

I'm not sure how your DTD is going to help you, because it implies a set of unrelated and overlapping Java objects from which the final XML will be created.

To see what I mean, try it for yourself.

Using the xjc tool (see here), you can generate Java objects from your DTD:

/path/to/jdk/bin/xjc -d output -p org.ajames.jaxbdemo.points -dtd my_schema.dtd

Using these Java classes, you can populate your data structure (your listOfPoints). And then you can create the XML output from that (as shown above, using the JAXB marshaller).

But your DTD will create some not-very-useful objects...

So, that comes back to the initial question: What do you want your XML to look like?

3) Use an XSD?

If you manually create a sample of your required XML, you can then generate an XSD from it. There are various online tools to help with that. Then you can use the xjc command again (but this time for an XSD not a DTD). And then you can use the resulting Java objects to get the exact XML you need.



来源:https://stackoverflow.com/questions/61255664/how-to-generate-xml-file-with-an-existing-dtd-for-sample-of-data-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!