26

刚从我的 XML 课程开始,在找到解决我的错误的方法时遇到了一些麻烦。我收到此错误:“根元素之后的文档中的标记必须格式正确。起始位置:6:2”。

任何形式的提示或帮助都会很棒。

 <?xml version="1.0" encoding="UTF-8"?>
 <heading>
 The Twelve Days of Christmas
 </heading>

<song> //OxyGen highlights "<song>" as the error, but i'm not sure I understand.
    <Day ="One"> 
        <verse>
    On the first day of Christmas, my true love sent to me, a partridge in a pear tree.
        </verse>
    </Day>

    <Day="Two">
        <verse>
     On the second day of Christmas, my true love sent to me 
     Two turtle doves 
     and a partridge in a pear tree. 
        </verse>
    </Day>

     <Day="Three"> 
         <verse>
     On the third day of Christmas, my true love sent to me 
     Three French hens, 
     two turtle doves 
     And a partridge in a pear tree.
         </verse>
     </Day>

     <Day="Four">
         <verse>
     On the fourth day of Christmas, my true love sent to me 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree. 
         </verse>
     </Day>

     <Day="Fifth">
         <verse>
     On the fifth day of Christmas, my true love sent to me 
     Five golden rings. 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree.
         </verse>
     </Day>

     <Day="Sixth">
         <verse>
     On the sixth day of Christmas, my true love gave to me 
     Six geese a-laying, 
     Five golden rings. 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree. 
         </verse>
     </Day>

     <Day="Seventh">
         <verse>
     On the seventh day of Christmas, my true love gave to me 
     Seven swans a-swimming, 
     six geese a-laying, 
     Five golden rings. 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree. 
         </verse>
     </Day>

     <Day="Eighth">
         <verse>
     On the eighth day of Christmas, my true love gave to me 
     Eight maids a-milking, 
     seven swans a-swimming, 
     six geese a-laying, 
     Five golden rings. 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree. 
         </verse>
     </Day>

     <Day="Ninth">
         <verse>
     On the ninth day of Christmas, my true love gave to me 
     Nine ladies dancing, 
     eight maids a-milking, 
     seven swans a-swimming, 
     six geese a-laying, 
     Five golden rings. 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree. 
         </verse>
     </Day>

     <Day="Tenth">
         <verse>
     On the tenth day of Christmas, my true love gave to me 
     Ten lords a-leaping, 
     nine ladies dancing, 
     eight maids a-milking, 
     seven swans a-swimming, 
     six geese a-laying, 
     Five golden rings. 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree. 
         </verse>
     </Day>

     <Day="Eleventh">
         <verse>
     On the eleventh day of Christmas, my true love gave to me 
     Eleven pipers piping, 
     ten lords a-leaping, 
     nine ladies dancing, 
     eight maids a-milking, 
     seven swans a-swimming, 
     six geese a-laying, 
     Five golden rings. 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree. 
         </verse>
     </Day>

     <Day="Twelfth">
         <verse>
     On the twelfth day of Christmas, my true love gave to me 
     Twelve drummers drumming, 
     eleven pipers piping, 
     ten lords a-leaping, 
     nine ladies dancing, 
     eight maids a-milking, 
     seven swans a-swimming, 
     six geese a-laying, 
     Five golden rings. 
     Four calling birds, 
     three French hens, 
     two turtle doves 
     And a partridge in a pear tree. 
         </verse>
     </Day>

 </song>
4

2 回答 2

60

在 XML 中只能有一个根元素——你有两个——headingsong.

如果您重组为:

<?xml version="1.0" encoding="UTF-8"?>
<song> 
 <heading>
 The Twelve Days of Christmas
 </heading>
 ....
</song>

关于根级别格式正确的 XML 的错误应该会消失(尽管可能存在其他问题)。

于 2012-09-18T10:23:45.923 回答
-4

确保字符串“strOutput”具有正确的 XML 结构后,您可以执行以下操作:

Matcher junkMatcher = (Pattern.compile("^([\\W]+)<")).matcher(strOutput);
strOutput = junkMatcher.replaceFirst("<");
于 2014-03-03T06:32:37.600 回答