gpt4 book ai didi

java - DocumentBuilder tagName 根本问题

转载 作者:行者123 更新时间:2023-12-01 18:31:31 25 4
gpt4 key购买 nike

我正在尝试使用 DocumentBuilder 并解析一个包含多个文档的大文件。当我运行程序时,出现此错误:"The markup in the document following the root element must be well-formed."

我认为这是因为我的文档上没有实际的根目录,它是一个 TextEdit Document ,结构如下:

<DOC>
<DOCNO> AP890106-0001 </DOCNO>
<FILEID>AP-NR-01-06-89 0033EST</FILEID>
<FIRST>r a PM-BRF--Heidnik 01-06 0136</FIRST>
<SECOND>PM-BRF--Heidnik,0139</SECOND>
<HEAD>Torture-Murderer In Fair Condition, Conscious</HEAD>
<DATELINE>PITTSBURGH (AP) </DATELINE>
<TEXT>
Convicted torture-murderer Gary Heidnik has
regained consciousness after apparently attempting suicide in his
prison cell with a drug overdose, prison officials said.
Heidnik's condition was upgraded to fair Thursday, but he
remained under tight security in the intensive care unit of West
Penn Hospital, said Tom Seiverling, a spokesman for the State
Correctional Institution at Pittsburgh.
Heidnik, 45, was semi-comatose earlier this week after being
found unconscious in his cell Sunday. Prison officials believe
Heidnik stored up medications that were prescribed for him by
pretending to take them at the designated times.
The self-proclaimed minister faces the death sentence for the
slayings of two of six women he kept chained in the basement of his
Philadelphia row house. He was convicted and sentenced last July.
</TEXT>
</DOC>
<DOC>
<DOCNO> AP890106-0002 </DOCNO>
<FILEID>AP-NR-01-06-89 0524EST</FILEID>
<FIRST>d a PM-BRF--DrivingToddler 01-06 0162</FIRST>
<SECOND>PM-BRF--Driving Toddler,0166</SECOND>
<HEAD>3-Year-Old Takes Careening First Drive; Emerges Unharmed</HEAD>
<DATELINE>CAZENOVIA, N.Y. (AP) </DATELINE>
<TEXT>
Going out to buy a puppy, Cecilia Kaler
placed her three-year-old son in a child seat, left the car running
and got out to clear snow from the windshield. She never finished
the job.
As soon as his mother closed the door, little Michael Kaler
locked it, put the car in drive, and rode away Wednesday. The car
went down the driveway, across a busy road, narrowly missed a tree
and fire hydrant, rolled on its side down an embankment and finally
came to rest in a creek.
Michael was wet, cold and otherwise unharmed, said Kaler, a
resident of this community 15 miles southeast of Syracuse.
A nearby man heard Kaler screaming and rushed over. He smashed a
window and freed little Michael.
``Anybody who says there's no God doesn't know what they're
talking about, because someone certainly was looking out for him,''
Kaler said Thursday.
</TEXT>
</DOC>

我想用 tagNames <DOC> 分隔每个文档和</DOC>

到目前为止我的代码:

 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(document);
doc.getElementsByTagName("doc").toString();

最佳答案

无法解析您的文件,因为没有“唯一”根元素。您的

<doc> </doc> 

block 必须用另一个标签容器包围:选择您喜欢的名称。然后,当 xml 格式良好时,您可以尝试解析。

示例:

<mytag>
<doc> ........</doc>
<doc>........... </doc>
</mytag>

关于java - DocumentBuilder tagName 根本问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60157575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com