简体   繁体   中英

Premature end of file. org.xml.sax.SAXParseException; Premature end of file.[Fatal Error] :-1:-1:

I'm trying to insert some elements into henan-dishes.xml , What can I do to solve the question I raised?

I checked my xml , but I found nothing wrong!

package com.jerry.xmlwriter;

import com.jerry.common.XMLConfigUtils;
import com.jerry.common.XmlTag;
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;

/**
 * DOM
 *
 * @author Jerry Hsu
 */
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");
    }
}

The following is the content of henan-dishes.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<menu>
    <food>
        <name>鱼香肉丝</name>
        <price>18¥</price>
        <description>经典川菜</description>
    </food>
    <food>
        <name>鲤鱼焙面</name>
        <price>59¥</price>
        <description>开封名菜</description>
    </food>
</menu>
  [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)
    at com.jerry.common.XMLConfigUtils.getDocument(XMLConfigUtils.java:57)
    at com.jerry.xmlwriter.XmlWriterByDom.xmlInsert(XmlWriterByDom.java:29)
    at com.jerry.xmlwriter.XmlWriterByDom.main(XmlWriterByDom.java:71)
Exception in thread "main" java.lang.NullPointerException
    at com.jerry.xmlwriter.XmlWriterByDom.xmlInsert(XmlWriterByDom.java:32)
    at com.jerry.xmlwriter.XmlWriterByDom.main(XmlWriterByDom.java:71)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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