Reputation: 67
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. 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.
Here is my xml file:
<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. Is there a way I format it within the xml file or parse it differently so I don't receive the error?
Upvotes: 1
Views: 1160
Reputation: 484
&
is a special character in XML
and stands for the start of a character reference. If you want to use an ampersand, you have to reference to it using &
Hence you XML file should look like the following:
<Feeds>
<item>
<name>Example-Name</name>
<url>http://someurl.com/process.php?appuser=[USER]&auth=[KEY]</url>
<frequency>5</frequency>
</item>
</Feeds>
Upvotes: 2