简体   繁体   English

StAX Parser 创建一个空的 XML 文件

[英]StAX Parser creates an empty XML file

I'm learning parsers in Java and now I'm trying to write a code for XML-file creation using StAX Parser and XMLStreamWriter and StringWriter.我正在学习 Java 中的解析器,现在我正在尝试使用 StAX Parser 和 XMLStreamWriter 以及 StringWriter 编写用于 XML 文件创建的代码。 But I forced with a problem that my parser creates an empty XML-file.但是我遇到了一个问题,即我的解析器创建了一个空的 XML 文件。 I think the problem is in using for-loop, but I don't know how to fix that.我认为问题在于使用 for 循环,但我不知道如何解决。 Here's a snippet of a code:这是一段代码:

public class STAXParser4 {
...
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException { 
...
        try (FileWriter fileWriter = new FileWriter("STAX1ShoesShop.xml")){
            StringWriter stringWriter = new StringWriter();
            XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
            XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(stringWriter);
            
            xmlStreamWriter.writeStartDocument();
            xmlStreamWriter.writeStartElement("ss:ShoesShop");
            ShoesShop shoesShop = new ShoesShop();
            
            for(Shoes shoes: shoesShop.getShoes()){ 
                xmlStreamWriter.writeStartElement("ss:shoes");
                xmlStreamWriter.writeAttribute("id", String.valueOf(shoes.getId()));
                
                xmlStreamWriter.writeStartElement("ss:title");
                xmlStreamWriter.writeCharacters(shoes.getTitle());
                xmlStreamWriter.writeEndElement();
                
                xmlStreamWriter.writeStartElement("ss:brand");
                xmlStreamWriter.writeCharacters(shoes.getBrand().toString());
                xmlStreamWriter.writeEndElement();
                
                xmlStreamWriter.writeStartElement("ss:category");
                xmlStreamWriter.writeCharacters(shoes.getCategory().toString());
                xmlStreamWriter.writeEndElement();
                
                xmlStreamWriter.writeStartElement("ss:season");
                xmlStreamWriter.writeCharacters(shoes.getSeason().toString());
                xmlStreamWriter.writeEndElement();
                
                xmlStreamWriter.writeStartElement("ss:price");
                xmlStreamWriter.writeCharacters(String.valueOf(shoes.getPrice()));
                xmlStreamWriter.writeEndElement();
                
                xmlStreamWriter.writeEndElement();
            } 
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeEndDocument();
            
            xmlStreamWriter.flush();
            xmlStreamWriter.close();
        } catch (XMLStreamException | IOException e) {
            e.printStackTrace();
        }
        
    }
}

Also here's a Java class for Shoes这里还有一个用于鞋类的 Java 类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Shoes", propOrder = {
    "title",
    "brand",
    "category",
    "season",
    "gender",
    "details",
    "price"
})
public class Shoes
    extends Entity
{
    @XmlElement(required = true)
    protected String title;
    @XmlElement(required = true)
    @XmlSchemaType(name = "string")
    protected Brand brand;
    @XmlElement(required = true)
    @XmlSchemaType(name = "string")
    protected Category category;
    @XmlElement(required = true)
    @XmlSchemaType(name = "string")
    protected Season season;
    @XmlElement(required = true)
    protected Shoes.Gender gender;
    @XmlElement(required = true)
    protected Shoes.Details details;
    protected double price;
    @XmlAttribute(name = "stock", required = true)
    protected boolean stock;
    @XmlAttribute(name = "mostWanted")
    protected Boolean mostWanted;

    public String getTitle() {
        return title;
    }

    public void setTitle(String value) {
        this.title = value;
    }
    
    public Brand getBrand(){
        return brand;
    }
    
    public void setBrand(Brand value){
        this.brand = value;
    }
    
    public Category getCategory(){
        return category;
    }
    
    public void setCategory(Category value){
        this.category = value;
    }
    
    public Season getSeason(){
        return season;
    }
    
    public void setSeason(Season value) {
        this.season = value;
    }

    public Shoes.Gender getGender() {
        return gender;
    }

    public void setGender(Shoes.Gender value) {
        this.gender = value;
    }

    public Shoes.Details getDetails() {
        return details;
    }

    public void setDetails(Shoes.Details value) {
        this.details = value;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double value) {
        this.price = value;
    }

    public boolean isStock() {
        return stock;
    }

    public void setStock(boolean value) {
        this.stock = value;
    }

    public Boolean isMostWanted() {
        return mostWanted;
    }

    public void setMostWanted(Boolean value) {
        this.mostWanted = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {

    })
    public static class Details {

        @XmlElement(required = true)
        protected String highlights;
        @XmlElement(required = true)
        protected String composition;

        public String getHighlights() {
            return highlights;
        }

        public void setHighlights(String value) {
            this.highlights = value;
        }

        public String getComposition() {
            return composition;
        }

        public void setComposition(String value) {
            this.composition = value;
        }

    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "\u043c\u0443\u0436\u0441\u043a\u043e\u0439Or\u0416\u0435\u043d\u0441\u043a\u0438\u0439"
    })
    public static class Gender {

        @XmlElementRefs({
            @XmlElementRef(name = "\u0436\u0435\u043d\u0441\u043a\u0438\u0439", namespace = "http://www.example.org/ShoesShop", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "\u043c\u0443\u0436\u0441\u043a\u043e\u0439", namespace = "http://www.example.org/ShoesShop", type = JAXBElement.class, required = false)
        })
        protected List<JAXBElement<String>> maleOrFemale;

        public List<JAXBElement<String>> getMaleOrFemale() {
            if (maleOrFemale == null) {
                maleOrFemale = new ArrayList<JAXBElement<String>>();
            }
            return this.maleOrFemale;
        }
    }
}

The data for Shoes shoes: shoesShop.getShoes I get from another XML-file.鞋子的数据: shoesShop.getShoes 我从另一个 XML 文件中获得。 Here's a snippet of this file:这是该文件的一个片段:

<ss:ShoesShop xmlns:ss="http://www.example.org/ShoesShop" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.example.org/ShoesShop ShoesShop.xsd ">
      <ss:shoes id="1" stock="true">
        <ss:title>Baltrum</ss:title>
        <ss:brand>Gucci</ss:brand>
        <ss:category>Boots</ss:category>
        <ss:season>fall</ss:season>
        <ss:price>734.0</ss:price>
      </ss:shoes>
  <ss:shoes id="2" stock="true" mostWanted = "true">
    <ss:title>Amalfi</ss:title>
    <ss:brand>Dior</ss:brand>
    <ss:category>Mules</ss:category>
    <ss:season>winter</ss:season>
    <ss:price>364.0</ss:price>
  </ss:shoes>
</ss:ShoesShop>

The problem is that you are writing the output into stringWriter but it is in no way connected to your output file.问题是您正在将输出写入stringWriter但它与您的输出文件没有任何联系。
Try replacing the stringWriter with fileWriter and you will see xml written to the output file:尝试用stringWriter替换fileWriter ,您将看到 xml 写入输出文件:

    try (FileWriter fileWriter = new FileWriter("STAX1ShoesShop.xml")){
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(fileWriter);
   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM