Java XML 解析错误:org.xml.sax.SAXParseException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19108050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:09:48  来源:igfitidea点击:

XML Parse ERROR : org.xml.sax.SAXParseException

javaxmlxml-parsing

提问by Priyan RockZ

here shows my error

这里显示了我的错误

[Fatal Error] designations.xml:1:15: Open quote is expected for attribute "{1}" associated with an  element type  "value".
org.xml.sax.SAXParseException; systemId: file:/home/priyan/hr_openerp/XMLParserPro/src/com/priyan/designations.xml; lineNumber: 1; columnNumber: 15; Open quote is expected for attribute "{1}" associated with an  element type  "value".
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:251)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:300)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
    at com.priyan.XmlParserMain.main(XmlParserMain.java:20)

here shows my code

这里显示了我的代码

public class XmlParserMain {
    public static void main(String argv[]) {
        try {
            File fXmlFile = new File("/home/priyan/hr_openerp/XMLParserPro/src/com/priyan/designations.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);//ERROR COMES IN THIS LINE
            doc.getDocumentElement().normalize();
            System.out.println("Root element :"+ doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("staff");
            System.out.println("----------------------------");
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    System.out.println("Designation: "+ eElement.getAttribute("OPTION"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

here is my xml file which i'm going to parse

这是我要解析的 xml 文件

<designations>
<OPTION value=3D777>3D Graphic Designer</OPTION>
<OPTION value=3D382>Account Executive</OPTION>
<OPTION value=3D108>Account Manager</OPTION>
<OPTION = value=3D1>Accountant</OPTION>
<OPTION = value=3D501>Accountant Inventory to Accountant = Payble
</OPTION>
<OPTION value=3D304>Accountant Payable</OPTION>
<OPTION value=3D84>Accounts Assistant</OPTION>

采纳答案by user2339071

Change the valueattribute in the optiontag. You need to have quotes surrounding the valueof your id.

更改标签中的value属性option。您需要在您的 id值周围加上引号

<option value='id'>XYZ</option>

OR

或者

<option value="id">XYZ</option>

You can use either of the quotations. Single or double.

您可以使用其中任何一个引号。单人或双人。

For reference check: XML Attributes

参考检查:XML 属性

Hope it helps.:)

希望能帮助到你。:)

回答by stallion

All the attribute values of an xml tag should be enclosed with quotes. So your Value attribute should be enclosed with quotes

xml 标签的所有属性值都应该用引号括起来。所以你的 Value 属性应该用引号引起来

Example :

例子 :

<person sex="female">
  <firstname>Anna</firstname>
  <lastname>Smith</lastname>
</person>