简体   繁体   English

用特殊字符读取Java中的xml文件

[英]Reading xml file in java with a special character

I'm attempting to create an engine that stores several references to RSS feeds for the purpose of compiling them myself to then create my own custom feeds. 我试图创建一个引擎,用于存储对RSS提要的多个引用,以便自己进行编译,然后创建自己的自定义提要。 I'm saving the feed data in an xml file then reading it in my java engine and storing a URL as a simple string. 我将提要数据保存在一个xml文件中,然后在我的Java引擎中读取它,并将URL存储为简单字符串。

Here is my xml file: 这是我的xml文件:

<Feeds>
<item>
    <name>Example-Name</name>
    <url>http://someurl.com/process.php?appuser=[USER]&auth=[KEY]</url>
    <frequency>5</frequency>
</item>
</Feeds>

I read this file with this: 我阅读此文件与此:

try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;

        dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xmlFile);
        doc.getDocumentElement().normalize();

        NodeList list = doc.getElementsByTagName("item");

        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            Element e = (Element) n;
            String fn = e.getElementsByTagName("name").item(0).getTextContent();
            String fu = e.getElementsByTagName("url").item(0).getTextContent();
            int ff = Integer.parseInt(e.getElementsByTagName("frequency").item(0).getTextContent());
            addFeed(fn, fu, ff);
        }
    } catch (ParserConfigurationException e1) {
        System.out.println("Parse Error");
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (SAXException e1) {
        System.out.println("SAX Error");
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        System.out.println("IO Error");
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

And I'm getting this error: 我收到此错误:

[Fatal Error] Feeds.xml:15:75: The reference to entity "auth" must end with the ';' delimiter.
SAX Error
org.xml.sax.SAXParseException; systemId: file:/C:/Feed%20Engine/Feeds.xml; lineNumber: 15; columnNumber: 75; The reference to entity "auth" must end with the ';' delimiter.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at main.FeedFetcher.<init>(FeedFetcher.java:28)
at main.FEMain.main(FEMain.java:10)

Ultimately it seems when reading the xml file, "auth" is being picked up as a keyword. 最终看来,当读取xml文件时,“ auth”被用作关键字。 Is there a way I format it within the xml file or parse it differently so I don't receive the error? 有没有一种方法可以在xml文件中设置其格式或以其他方式解析它,以使我不会收到错误?

& is a special character in XML and stands for the start of a character reference. &XML的特殊字符,代表字符引用的开始。 If you want to use an ampersand , you have to reference to it using &amp; 如果要使用&符号 ,则必须使用&amp;引用它&amp;

Hence you XML file should look like the following: 因此,您的XML文件应如下所示:

<Feeds>
 <item>
  <name>Example-Name</name>
  <url>http://someurl.com/process.php?appuser=[USER]&amp;auth=[KEY]</url>
  <frequency>5</frequency>
 </item>
</Feeds>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM