1

私はJAXBマーシャリングを使用してXMLファイルを作成していますが、正常に作成され、JAXBアンマーシャリングを使用してこれらのファイルを表示したいことがわかっています。使用しているコードは次のとおりです。

public Object display(String fileName) throws IOException, JAXBException {

        XmlStructure object;
        File file = new File(fileName);
        JAXBContext jaxbContext = JAXBContext.newInstance(XmlStructure.class);
        Unmarshaller jaxbUnMarshaller = jaxbContext.createUnmarshaller();

        object = (XmlStructure) jaxbUnMarshaller.unmarshal(file);

        System.out.println(object.toString());
        return object;

    }

前のコードは私にその結果を与えます:

com.nc.inotify.dp.xml.impl.XmlStructure@2916a6bf

そして私はそれでコードを変更しました:

    public Object display(String fileName) throws IOException, JAXBException {
XmlStructure object;
        File file = new File(fileName);
        JAXBContext jaxbContext = JAXBContext.newInstance(XmlStructure.class);
        Unmarshaller jaxbMarshaller = jaxbContext.createUnmarshaller();

        object = (XmlStructure) jaxbMarshaller.unmarshal(file);

        Marshaller jaxbMarshallerz = jaxbContext.createMarshaller();
        jaxbMarshallerz.marshal(object, System.out);

        return object;
}

しかし、それは私にその結果を与えます:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><XmlSource/>

これが XML ファイルです。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<XmlSource>
<XmlConf>
    <hostName>weather.yahooapis.com</hostName>
    <parameters>
        <entry>
            <key>w</key>
            <value>2502265</value>
        </entry>
    </parameters>
    <URLPath>/forecastrss</URLPath>
    <urlProtocol>http</urlProtocol>
</XmlConf>
<XmlConf>
    <hostName>weather.yahooapis.com</hostName>
    <parameters>
        <entry>
            <key>w</key>
            <value>2553822</value>
        </entry>
    </parameters>
    <URLPath>/forecastrss</URLPath>
    <urlProtocol>http</urlProtocol>
</XmlConf>
</XmlSource>

更新 **: そのフォームを取得するために、マーシャリング プロセスで複数のクラスを使用しています。マーシャリング メソッド:

public void add(String fileName) throws IOException, JAXBException,
            ParserConfigurationException, SAXException, TransformerException {

        this.fileName = fileName; 
        File temp = new File(tempName);
        JAXBContext jaxbContext = JAXBContext.newInstance(XmlConfList.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        File source = new File(fileName);
        if (source.exists()) {

            jaxbMarshaller.marshal(object, temp);
            MergeXml merge = new MergeXml();
            merge.mergeXML(true, fileName, tempName, mainTag);
        } else {
            XmlStructure struct = new XmlStructure();
            jaxbMarshaller.marshal(struct, source);
            jaxbMarshaller.marshal(object, temp);
            MergeXml merge = new MergeXml();
            merge.mergeXML(true, fileName, tempName, mainTag);
        }
        temp.delete();
    }

MergeXml クラス:

public class MergeXml {

    private static final String YES = "yes";
    private static final String generalTag = "*";

    /**
     * This method used to merge XML old and new files together
     */
    public void mergeXML(boolean condition, String fileName, String tempName, String mainTag)
            throws ParserConfigurationException, SAXException, IOException,
            TransformerException {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        Document doc = null;
        Document doc2 = null;

        db = dbf.newDocumentBuilder();
        doc = db.parse(new File(fileName));
        doc2 = db.parse(new File(tempName));

        NodeList elements = doc.getElementsByTagName(mainTag);

        if (condition == true) {
            NodeList nodeList = doc2.getElementsByTagName(generalTag);

            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);
                Node childNode = doc.adoptNode(node);

                elements.item(0).appendChild(childNode);
            }

        }

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, YES);

        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(source, result);

        BufferedWriter output = new BufferedWriter(new FileWriter(fileName));
        String xmlOutput = result.getWriter().toString();
        output.write(xmlOutput);
        output.close();

    }
}

XmlStructure クラス:

@XmlRootElement(name = "XmlSource")
public class XmlStructure{

}

XmlConf クラス:

@XmlRootElement(name = "XmlConf")
public class XmlConf extends XmlStructure {

    private String URLProtocol;
    private List<String> path = new ArrayList<String>();
    private String urlp;
    private Map<String, String> parameters;
    private String host;

    /**
     * This method used to retrieve the specified URL protocol
     * @return {@code String} 
     */
    public String getUrlProtocol() {
        return URLProtocol;
    }

    /**
     * This method used to store the URL protocol as String if the URL is a valid one
     * @param URL Protocol
     *            
     */
    @XmlElement
    public void setUrlProtocol(String URLProtocol) {
        this.URLProtocol = URLProtocol;
    }

    /**
     *  This method used to retrieve all the paths selected
     *         by the user in order to save
     * @return {@code List<String>}
     */
    @XmlElement
    public List<String> getPath() {
        return path;
    }

    /**
     * This method used to store a new path added by the user
     * @param path
     *            
     */
    public void setPath(String path) {
        this.path.add(path);
    }

    /**
     * This method used to set the path of the specified URL
     * @param urlp
     *            
     */
    @XmlElement(name = "URLPath")
    public void setUrlPath(String urlp) {
        this.urlp = urlp;
    }

    /**
     * This method used to retrieve the path of the specified URL
     * @return {@code String} 
     */
    public String getUrlPath() {
        return urlp;
    }

    /**
     * This method used to set the parameters of the specified URL
     * @param parameters
     *            
     */
    @XmlElementWrapper
    public void setParameters(Map<String, String> parameters) {
        this.parameters = parameters;
    }

    /**
     * This method used to retrieve the parameters
     *         of the specified URL
     * @return {@code Map<String, String>} 
     */
    public Map<String, String> getParameters() {
        return parameters;
    }

    /**
     *  This method used to set the host name of the specified URL
     * @param host
     *           
     */
    public void setHostName(String host) {
        this.host = host;
    }

    /**
     * This method used to retrieve the host name of the
     *         specified URL
     * @return {@code String} 
     */
    public String getHostName() {
        return host;
    }
}

XmlConfList クラス:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlConfList {

    @XmlElementWrapper(name = "XmlSource")
    @XmlElement(name = "XmlConf")
    private List<XmlConf> list = null;

    public List<XmlConf> getList() {
        if(this.list == null)
            this.list = new ArrayList<>();
        return this.list;
    }
}
4

0 に答える 0