用dom4j解析xml报错: Error on line 1 of document : Content is not allowed in prolog。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用dom4j解析xml报错: Error on line 1 of document : Content is not allowed in prolog。相关的知识,希望对你有一定的参考价值。
java:
public class Dom4jTest
public static void main(String[] args) throws DocumentException
String xml="D:\\test.xml";
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xml);
document = DocumentHelper.parseText(xml);//报错行!!
List<?> list = document.selectNodes("//article" );
Iterator iter=list.iterator();
while(iter.hasNext())
Element element=(Element)iter.next();
System.out.println(element.elementText("title"));
test.xml:
<?xml version="1.0"?>
<catalog>
<journal>
<article>
<title>Java configuration with XML Schema</title>
<author>
<firstname>Marcello</firstname>
<lastname>Vitaletti</lastname>
</author>
</article>
</journal>
</catalog>
PS:1.网上用UE改变xml编码格式的方法无效。 2.手动重写xml文件防止汉英输入法导致的xml文件标准问题,没有问题。3.按提示添加了所有可能应用到的jar包,还是解决不了。唉。。。神在哪?
没用,之前有的,测试不行。这里忘加上去了
参考技术B dom4j解析的,个人还是觉得XStream解析这个更快,下面是dom4j解析import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.byc.app.interationFlight.model.verifyPriceModel.StandPriceModel;
public class Turing
public static void main(String[] args)
try
SAXReader saxReader = new SAXReader();// 读取文件
Document doc = saxReader
.read("C:\\Users\\byc\\Desktop\\checkFlightCabin_response.xml");// 读取文件
// Document doc = DocumentHelper.parseText(xmlData);//读取字符串
Element rootElt = doc.getRootElement();
StandPriceModel um = new StandPriceModel();
Iterator bodyIter = rootElt.elementIterator();
while (bodyIter.hasNext())
Element itemEle = (Element) bodyIter.next();
System.out.println("--" + itemEle.getName());
Iterator bodyI = itemEle.elementIterator();
while (bodyI.hasNext())
Element element = (Element) bodyI.next();
Iterator bodyIt = element.elementIterator();
if (element.getName().equalsIgnoreCase("article"))
Iterator bodyIte = element.elementIterator();
while (bodyIte.hasNext())
Element eleme = (Element) bodyIte.next();
if (eleme.getName().equalsIgnoreCase("title"))
System.out.println(eleme.getStringValue());
if (eleme.getName().equalsIgnoreCase("author"))
Iterator body = eleme.elementIterator();
while (body.hasNext())
Element elem = (Element) body.next();
if (elem.getName().equalsIgnoreCase(
"firstname"))
System.out.println(elem
.getStringValue());
if (elem.getName().equalsIgnoreCase(
"lastname"))
System.out.println(elem
.getStringValue());
catch (DocumentException e)
// TODO Auto-generated catch block
e.printStackTrace();
直接copy即可本回答被提问者和网友采纳
以上是关于用dom4j解析xml报错: Error on line 1 of document : Content is not allowed in prolog。的主要内容,如果未能解决你的问题,请参考以下文章
dom4j解析xml报错:Nested exception: org.xml.sax.SAXParseException: White space is required between the pr