0

I am using sax to create an xml file, but when I run the program, the following error occurs:

[Fatal Error] :-1:-1: Premature end of file.
org.xml.sax.SAXParseException; Premature end of file.
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)

I checked the files written, but I didn't see any problems.The following is my code and the xml file of the operation.

1. Code

The main logic of the following code is: create an XML node, and then append this node to the old file.


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

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 java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class XmlWriterByDom {

    private static final XMLConfigUtils xmlConfig = XMLConfigUtils.getInstance();

    public void xmlInsert(Map<String, String> xmlNode, String xmlPath) {
        Document doc = xmlConfig.getDocument(xmlPath);
        Text nodeValue;

        Element root = doc.getDocumentElement();
        Element food = doc.createElement(XmlTag.FOOD);
        Element name = doc.createElement(XmlTag.NAME);
        Element price = doc.createElement(XmlTag.PRICE);
        Element desc = doc.createElement(XmlTag.DESC);

        nodeValue = doc.createTextNode(xmlNode.get(XmlTag.FOOD));
        name.appendChild(nodeValue);
        food.appendChild(name);

        nodeValue = doc.createTextNode(xmlNode.get(XmlTag.PRICE));
        price.appendChild(nodeValue);
        food.appendChild(price);

        nodeValue = doc.createTextNode(xmlNode.get(XmlTag.DESC));
        desc.appendChild(nodeValue);
        food.appendChild(desc);

        root.appendChild(food);

        try {
            xmlPath = Objects.requireNonNull(               this.getClass().getClassLoader().getResource(xmlPath)).getPath();
            TransformerFactory transformer = TransformerFactory.newInstance();
            Transformer trans = transformer.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(xmlPath).toURI().getPath());
            trans.transform(source, result);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Map<String, String> xmlNode = new HashMap<>(3);
        xmlNode.put("name", "tomato");
        xmlNode.put("price", "$10");
        xmlNode.put("description", "dishes");

        new XmlWriterByDom().xmlInsert(xmlNode, "xml/henan-dishes.xml");
    }
}

2. XML

<?xml version="1.0" encoding="UTF-8" ?>
<menu>
    <food>
        <name>1</name>
        <price>18$</price>
        <description>2</description>
    </food>
    <food>
        <name>1</name>
        <price>59$</price>
        <description>2</description>
    </food>
</menu>
qingmu
  • 402
  • 2
  • 12
  • ok, I have attached My XML – qingmu Oct 20 '19 at 15:10
  • The XML seems to be OK, it could be parsed with Xerces DOM parser. Most likely the problem is the format of the file in the file system. This problem may come due to the byte order mark in the file. Refer https://stackoverflow.com/questions/1772321/what-is-xml-bom-and-how-do-i-detect-it – Ironluca Oct 20 '19 at 15:33
  • When I try to delete the compiled folder with the target name and re-execute, the result is correct.May be really a problem with the file system! – qingmu Oct 21 '19 at 02:49

1 Answers1

0

When I try to delete the compiled folder with the target name and re-execute, the result is correct. May be really a problem with the file system! Project structure

qingmu
  • 402
  • 2
  • 12