简体   繁体   English

文件过早结束。 org.xml.sax.SAXParseException; 文件过早结束。[致命错误]:-1:-1:

[英]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?我正在尝试在henan-dishes.xml中插入一些元素,我该怎么做才能解决我提出的问题?

I checked my xml , but I found nothing wrong!我检查了我的xml ,但没有发现任何问题!

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 :以下是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项目结构

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

相关问题 org.xml.sax.SAXParseException:*VALID* XML 的文件过早结束 - org.xml.sax.SAXParseException: Premature end of file for *VALID* XML org.xml.sax.SAXParseException:过早的结束文件 - org.xml.sax.SAXParseException: Premature end file 轴:faultString:org.xml.sax.SAXParseException:文件过早结束 - Axis: faultString: org.xml.sax.SAXParseException: Premature end of file org.xml.sax.SAXParseException:带有jaxbUnmarshaller的文件过早结束 - org.xml.sax.SAXParseException: Premature end of file with jaxbUnmarshaller 引起原因:org.xml.sax.SAXParseException:文件过早结束 - Caused by: org.xml.sax.SAXParseException: Premature end of file org.xml.sax.SAXParseException:文件过早结束 - org.xml.sax.SAXParseException: Premature end of file org.xml.sax.SAXParseException:文件过早结束 - org.xml.sax.SAXParseException: Premature end of file 解析RSS时出错-&gt; org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1; 文件过早结束 - Error Parsing RSS -> org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file org.xml.sax.SAXParseException; 尝试从请求中获取 xml 输入时文件过早结束 - org.xml.sax.SAXParseException; Premature end of file when trying to get an xml input from request Java JaxB Unmarshaller提供org.xml.sax.SAXParseException文件的过早结尾 - Java JaxB Unmarshaller giving org.xml.sax.SAXParseException Premature end of file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM