如何从XML计算值,然后使用Java用计算出的值创建新的XML

时间:2018-09-14 07:11:13

标签: java xml

我正在尝试读取dvd.xml,然后打印出summary.xml。我想从dvd.xml中获取release_year,然后按十年计数。随着十年的发展,我想编写summary.xml。你能帮忙吗?

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import java.io.FileWriter;
import java.io.*;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Dvdinfo{

    public static void main(String[] args)
    {
        Dvdinfo summary = new Dvdinfo();
        summary.readXML();
        summary.writeXML();
    }

    private void readXML()
    {
        Document doc = null;
        try{

            String filename = "dvd.xml";
            DocumentBuilderFactory docFactory =         DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            docBuilder = docFactory.newDocumentBuilder();
            doc = docBuilder.parse(filename);

        }
        catch (ParserConfigurationException e) 
        {
           e.printStackTrace();
        } 
        catch (SAXException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        if(doc != null)
        {
            NodeList list = doc.getElementsByTagName("release_year");
            for (int i=0; i < list.getLength(); i++)
            {
                Node node = list.item(i);
               Element el = (Element) node;
                Element cl = (Element) el.getElementsByTagName("release_year").item(0);
                System.out.println(cl);
            }
        }

    }

    private void writeXML(){
        String fn = "summary.xml";
        try{
            DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder newDoc = newFactory.newDocumentBuilder();

            Document decade = newDoc.newDocument();
            Element rootElement = decade.createElement("DVD");
            decade.appendChild(rootElement);

            Element summary = decade.createElement("summary");
            rootElement.appendChild(summary);

            Element year = decade.createElement("count");
            year.appendChild(decade.createTextNode("1"));
            summary.appendChild(year);

            Attr attr = decade.createAttribute("decade");
            year.setAttributeNode(attr);

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("http://xml.apache.org/xslt}indent-amount", "2");

            DOMSource source = new DOMSource(decade);
            StreamResult result = new StreamResult(new File(fn));
            transformer.transform(source, result);
            System.out.println("---Release Year Summary Available---");

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        }

    }
}

dvd.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DVDS SYSTEM "dvd.dtd">
<DVDS>
    <DVD id="1">
        <title>Witness</title>
        <performers>
            <performer>Harrison  Ford</performer>
            <performer>Kelly  McGillis</performer>
        </performers>
        <discs>1</discs>
        <price>7.99</price>
        <release_year>1985</release_year>
    </DVD>
    <DVD id="2">
        <title>Star Wars Episode V - The  Empire Strikes Back</title>
        <performers>
            <performer>Mark  Hamill</performer>
            <performer>Harrison  Ford</performer>
            <performer>Carrie  Fisher</performer>
        </performers>
        <discs>2</discs>
        <price>14.95</price>
        <release_year>1980</release_year>
    </DVD>
    <DVD id="3">
        <title>Searching</title>
        <performers>
            <performer>John Cho</performer>
            <performer>Debra Messing</performer>
            <performer>Joseph Lee</performer>
        </performers>
        <discs>2</discs>
        <price>19.95</price>
        <release_year>2018</release_year>
    </DVD>
</DVDS>

summary.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DVD>
    <summary>
        <count decade="">1</count>
    </summary>
</DVD>

我想我需要创建一个数组来接收来自dvd.xml的输入。对于writeXML(),我想我需要使用XMLstreamwriter。但是我不确定如何编写这些代码以及如何逐年计数。谢谢。

0 个答案:

没有答案