【问题标题】:StAX Parser Content is not allowed in prolog [duplicate]序言中不允许 StAX 解析器内容 [重复]
【发布时间】:2013-07-10 07:00:06
【问题描述】:

这是我的家庭任务。我不明白它想从我这里得到什么以及为什么它不起作用。它在 reader.next() 上失败。路径是对的。这是StAXParser的Java代码:

public class StAXParser {

private Dances dances;
private Dance currentDance;
private Dancer currentDancer;

public static void main(String[] args) {
    StAXParser stAXParser = new StAXParser();
    stAXParser.parse("DanceGroupConcerts.xml");
}

public Dances getDances() {
    return dances;
}

public StAXParser() {
}

public void parse (String xmlFileName){
    try {
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlFileName));
        int tag;
        String tagName;
        String text = new String();
        while (reader.hasNext()){
            tag = reader.next();

            switch (tag){
                case XMLStreamReader.START_ELEMENT:

                    tagName = reader.getLocalName();

                    if (tagName == "Dances")
                        dances = new Dances();
                    if (tagName.equals("Dance"))
                        currentDance = new Dance();
                    if (tagName.equals("Dancer"))
                        currentDancer = new Dancer();
                    break;

                case XMLStreamReader.CHARACTERS:
                    text = reader.getText();
                    break;

                case XMLStreamReader.END_ELEMENT:

                    tagName = reader.getLocalName();

                    if (tagName.equals("Dancer"))
                        currentDance.getDancers().add(currentDancer);
                    if (tagName.equals("Dance"))
                        dances.getDance().add(currentDance);
                    if (tagName.equals("Type"))
                        currentDance.setType(text);
                    if (tagName.equals("Scene"))
                        currentDance.setScene(text);
                    if (tagName.equals("NumberOfDancers"))
                        currentDance.setNumberOfDancers(NumberOfDancers.fromValue(text));
                    if (tagName.equals("Music"))
                        currentDance.setMusic(Music.fromValue(text));
                    if (tagName.equals("Name"))
                        currentDancer.setName(text);
                    if (tagName.equals("DanceTeam"))
                        currentDancer.setNameOfDanceTeam(text);
                    if (tagName.equals("Age"))
                        currentDancer.setAge(BigInteger.valueOf(Integer.valueOf(text)));
                    if (tagName.equals("Experience"))
                        currentDancer.setExperience(BigInteger.valueOf(Integer.valueOf(text)));
                    if (tagName.equals("Number"))
                        currentDance.setNumber(BigInteger.valueOf(Integer.valueOf(text)));

                    break;
            }


        }

    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
}

}

XSD:

    <?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:Dance="Dancers"
targetNamespace="Dancers">
  <simpleType name="Type">
    <restriction base="string"></restriction>
  </simpleType>

  <simpleType name="Scene">
    <restriction base="string"></restriction>
  </simpleType>

  <simpleType name="NumberOfDancers">
    <restriction base="string">
      <enumeration value="mass"></enumeration>
      <enumeration value="solo"></enumeration>
      <enumeration value="pair"></enumeration>
    </restriction>
  </simpleType>

  <simpleType name="Music">
    <restriction base="string">
      <enumeration value="phonogram"></enumeration>
      <enumeration value="alive"></enumeration>
    </restriction>
  </simpleType>

  <simpleType name="Number">
    <restriction base="integer"></restriction>
  </simpleType>

    <simpleType name="Name">
    <restriction base="string"></restriction>
  </simpleType>

  <simpleType name="DanceTeam">
    <restriction base="string"></restriction>
  </simpleType>

  <simpleType name="Age">
    <restriction base="integer"></restriction>
  </simpleType>

  <simpleType name="Experience">
    <restriction base="integer"></restriction>
  </simpleType>


  <complexType name="Dancer">
    <sequence>
      <choice>
        <element name="name" type="Dance:Name"></element>
        <element name="nameOfDanceTeam" type="Dance:DanceTeam"></element>
      </choice>
      <element name="age" type="Dance:Age"></element>
      <element name="experience" type="Dance:Experience"></element>
    </sequence>
  </complexType>

    <complexType name = "Dance">
     <sequence>
       <element name="type" type="Dance:Type"></element>
       <element name="scene" type="Dance:Scene"></element>
       <element name="numberOfDancers" type="Dance:NumberOfDancers"></element>
       <element name="music" type="Dance:Music"></element>
       <element name="dancers" type="Dance:Dancer" minOccurs="1" maxOccurs="unbounded"></element>
       <element name="number" type="Dance:Number"></element>
     </sequence>
    </complexType>


  <element name="Dances">
    <complexType>
        <sequence>
            <element name="Dance" type="Dance:Dance" minOccurs="1" maxOccurs="unbounded"></element>
        </sequence>
    </complexType>
  </element>
</schema>

和 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Dances>
<Dance>
    <Type>Folk</Type>

    <Scene>Dr. Frank N. Furter Major</Scene>

    <NumberOfDancers>mass</NumberOfDancers>

    <Music>phonogram</Music>

    <Dancer>
        <Name>Majenta</Name>
        <Age>25</Age>
        <Experience>25</Experience>
    </Dancer>

    <Dancer>
        <Name>Riff Raff</Name>
        <Age>2500</Age>
        <Experience>25</Experience>
    </Dancer>

    <Dancer>
        <Name>Columbia</Name>
        <Age>25</Age>
        <Experience>25</Experience>
    </Dancer>

    <Dancer>
        <DanceTeam>Guests</DanceTeam>
        <Age>2566</Age>
        <Experience>667</Experience>
    </Dancer>

    <Number>1</Number>
</Dance>
</Dances>

感谢您的帮助。对不起,如果我的英语太糟糕了。

【问题讨论】:

  • SAX 解析器运行良好。我在 Notepad++ 的 XML- 和 XSD- 文件中看不到奇怪的字符。它会显示它们吗?

标签: java xml stax


【解决方案1】:

问题在于您的 XML 文件。 &lt;?xml... 之前有一些(不可见的)内容会导致解析器失败。 请参阅此问题以获取可能的解决方案:Content is not allowed in Prolog SAXParserException

回答您的编辑:您可能不会在 Notepad++ 中将此视为可见字符 - 请查看以下网址以获取可能的解决方案:http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

【讨论】:

  • 另外,在 XSD 中(我假设它也在被解析)。由于粘贴的文件在&lt;?xml 之前显示空格,所以我会把钱放在那里。
  • 好的,我把这个加到代码中:BOMInputStream bomIN = new BOMInputStream(newFileInputStream(xmlFileName));if (bomIN.hasBOM() == false){ 看起来,里面没有奇怪的符号。
  • 哦。现在它通过了。谢谢!
猜你喜欢
  • 2014-07-23
  • 1970-01-01
  • 2023-04-10
  • 2016-10-11
  • 2020-07-03
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多