用dom4j解析xml报错: Error on line 1 of document : Content is not allowed in prolog。
java:publicclassDom4jTest{publicstaticvoidmain(String[]args)throwsDocumentException{S...
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包,还是解决不了。唉。。。神在哪? 展开
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包,还是解决不了。唉。。。神在哪? 展开
2个回答
展开全部
将头标记改为<?xml version="1.0" encoding="UTF-8"?>
追问
没用,之前有的,测试不行。这里忘加上去了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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即可
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即可
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询