Java Using Saxparser results in exception thrown - java

CODE:
public void parse(byte[] payload)
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler()
{
boolean eid = false;
boolean msg_id = false;
boolean date_time = false;
boolean temp = false;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
if (qName.equalsIgnoreCase("eid"))
{
eid = true;
}
if (qName.equalsIgnoreCase("msg_id"))
{
msg_id = true;
}
if (qName.equalsIgnoreCase("date_time"))
{
date_time = true;
}
if (qName.equalsIgnoreCase("temperature"))
{
temp = true;
}
}
public void characters(char ch[], int start, int length) throws SAXException
{
if (eid)
{
XMLMessagePacket.this.eid = new String(ch, start, length);
eid = false;
}
if (msg_id)
{
XMLMessagePacket.this.msgId = new String(ch, start, length);
msg_id = false;
}
if (date_time)
{
XMLMessagePacket.this.time = new String(ch, start, length);
date_time = false;
}
if (temp)
{
XMLMessagePacket.this.temperature.parseDouble(new String(ch, start, length));
temp = false;
}
}
};
saxParser.parse(new ByteArrayInputStream(payload), handler);
}
catch (Exception e)
{
e.printStackTrace();
}
}
XML
<?xml version="1.0"?>
<event>
<eid>345345</eid>
<msg_id>3242</msg_id>
<date_time>11342345</date_time>
<temperature>100</temperature>
</event>
Problem:
org.xml.sax.SAXParseException: Content is not allowed in prolog.

Just something to check: a friend once fought this "bug" all night. Is it possible your file has a Byte Order Mark at the beginning? Parsing it as a String might make it disappear. The default encoding for XML is UTF-8 (which does not require a BOM). It's just possible you're getting tripped up by something you can't see.

Related

Turn java XML SAX Parser to web app Tomcat

I've got a java SAX Parser for XML (we set the date, make URL reqest for this date and parse XML file). Now I need to turn this code to web app in Tomcat. I've imported all nessessary libraries, created artefacts, but don't know how to change code itself.\
Here is initial code
Handler:
public class UserHandler extends DefaultHandler {
boolean bName = false;
boolean bValue = false;
String result=" ";
#Override
public void startElement(String uri,
String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("Valute")) {
String CharCode = attributes.getValue("CharCode");
} else if (qName.equalsIgnoreCase("Name")) {
bName = true;
} else if (qName.equalsIgnoreCase("Value")) {
bValue = true;
}
}
#Override
public void endElement(String uri,
String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("Valute")) {
System.out.print(" ");
}
}
#Override
public void characters(char ch[], int start, int length) throws SAXException {
if (bName) {
result=(new String(ch, start, length)+" ");
bName = false;
} else if (bValue) {
result=result+(new String(ch, start, length));
bValue = false;
System.out.print(result);
}
}
}
Main:
public static void main(String[] args) throws MalformedURLException {
//Set the date dd.mm.yyyy
String date="12.08.2020";
String link ="http://www.cbr.ru/scripts/XML_daily.asp?date_req=";
URL url =new URL(link);
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
UserHandler userHandler = new UserHandler();
saxParser.parse(String.valueOf(url+date), userHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}

SAX Parser: How to read only from `Item` tag?

I am using SAX Parser to parse some XML content. Please check my code below.
public void parse(InputSource is, AppDataBean appDataBean) throws RuntimeException {
int limitCheck;
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
Log.d("SAX",appDataBean.getUrl());
DefaultHandler handler = new DefaultHandler() {
boolean title = false;
boolean link = false;
boolean author = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase(TITLE)) {
title = true;
}
if (qName.equalsIgnoreCase(LINK)) {
link = true;
}
if (qName.equalsIgnoreCase(AUTHOR)) {
author = true;
}
//Log.d("SAX","Start Element :" + qName);
}
public void endElement(String uri, String localName,
String qName)
throws SAXException {
}
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println(new String(ch, start, length));
if (title) {
Log.d("SAX","End Element :" + "First Name : "
+ new String(ch, start, length));
title = false;
}
if (link) {
Log.d("SAX","End Element :" + "Last Name : "
+ new String(ch, start, length));
link = false;
}
if (author) {
Log.d("SAX","End Element :" + "Nick Name : "
+ new String(ch, start, length));
author = false;
}
}
};
saxParser.parse(is, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
Below is how my XML will look like.
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<?xml-stylesheet type="text/xsl" href="rss.xsl"?>
<channel>
<title>MyRSS</title>
<atom:link href="http://www.example.com/rss.php" rel="self" type="application/rss+xml" />
<link>http://www.example.com/rss.php</link>
<description>MyRSS</description>
<language>en-us</language>
<pubDate>Tue, 22 May 2018 13:15:15 +0530</pubDate>
<item>
<title>Title 1</title>
<pubDate>Tue, 22 May 2018 13:14:40 +0530</pubDate>
<link>http://www.example.com/news.php?nid=47610</link>
<guid>http://www.example.com/news.php?nid=47610</guid>
<description>bla bla bla</description>
</item>
</channel>
</rss>
However in here, I nee to avoid the Channel tag and only read of the root tag is Item. Then only I can get the real content. How can I do this?
Update
As suggested by an answer, I tried using the SAX Parser with stack. Below is the code but still I no good, now it prints nothing for the First Name
public void parse(InputSource is, AppDataBean appDataBean) throws RuntimeException {
int limitCheck;
stack = new Stack<>();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
Log.d("SAX", appDataBean.getUrl());
DefaultHandler handler = new DefaultHandler() {
boolean title = false;
boolean link = false;
boolean author = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
Log.d("SAX", "localName: " + localName);
if(localName.equalsIgnoreCase("item"))
{
stack = new Stack<>();
stack.push(qName);
}
if (qName.equalsIgnoreCase(TITLE)) {
if(stack.peek().equalsIgnoreCase("item"))
{
title = true;
}
}
if (qName.equalsIgnoreCase(LINK)) {
link = true;
}
if (qName.equalsIgnoreCase(AUTHOR)) {
author = true;
}
//Log.d("SAX","Start Element :" + qName);
}
public void endElement(String uri, String localName,
String qName)
throws SAXException {
stack.pop();
}
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println(new String(ch, start, length));
if (title) {
Log.d("SAX", "End Element :" + "First Name : "
+ new String(ch, start, length));
title = false;
}
if (link) {
Log.d("SAX", "End Element :" + "Last Name : "
+ new String(ch, start, length));
link = false;
}
if (author) {
Log.d("SAX", "End Element :" + "Nick Name : "
+ new String(ch, start, length));
author = false;
}
}
};
saxParser.parse(is, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
Typically a SAX application will maintain a stack to hold context. On a startElement event, push the element name to the stack; on endElement pop it off the stack. Then when you get a startElement event for a title element, you can do stack.peek() to see what the parent of the title is.

Error while reading XML file

I got an error while trying to read this xml file:
<?xml version="1.0" encoding="UTF-8"?>
<job>
<id>B002</id>
<name>Name</name>
<time>every day 1:00</time>
</job>
It said:
org.xml.sax.SAXParseException; systemId: file:///D:/JobManagement.xml; lineNumber: 1; columnNumber: 20; A pseudo attribute name is expected. .
I searched on google and find out some ways to solve this problem but they did not work. I'm using SAX Parser code from Mykong.com in the following link :
https://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
I have to solved this as quick as I can so I do not have enough time to learn it. Please help me.
Above xml code is just part of my file.
public class JobManagementService {
public void ReadXMLFile() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("FIRSTNAME")) {
bfname = true;
}
if (qName.equalsIgnoreCase("LASTNAME")) {
blname = true;
}
if (qName.equalsIgnoreCase("NICKNAME")) {
bnname = true;
}
if (qName.equalsIgnoreCase("SALARY")) {
bsalary = true;
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("End Element :" + qName);
}
public void characters(char ch[], int start, int length) throws SAXException {
if (bfname) {
System.out.println("First Name : " + new String(ch, start, length));
bfname = false;
}
if (blname) {
System.out.println("Last Name : " + new String(ch, start, length));
blname = false;
}
if (bnname) {
System.out.println("Nick Name : " + new String(ch, start, length));
bnname = false;
}
if (bsalary) {
System.out.println("Salary : " + new String(ch, start, length));
bsalary = false;
}
}
};
saxParser.parse("D:\\JobManagement.xml", handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I call ReadXMLFile method here
#GetMapping("/jobManagement")
public String home() {
jobManagementService.ReadXMLFile();
return "/jobManagement";
}
I just want to test this function before applying it
The file you have shown us is well-formed and parseable. Because the error message is very precise about what is wrong and where (just after the version="1.0") my suspicion would be that one of the characters isn't what it appears to be. Perhaps the quotation marks are actually "smart quotes", or perhaps the space after version="1.0" is actually a non-breaking space.
(By the way, telling us you are in a hurry and don't want to investigate the problem thoroughly will put many people off from answering your question. We're problem-solvers by nature, we like to get to the bottom of things, and working with someone who says they don't want to put any effort in from their side is not usually very rewarding.)

XML SAX parsing doesn't returned the desired result

I have a XML provided by my client like this:
<?xml version='1.0' encoding='UTF-8' ?><quizzes updatetimestamp='2012-08-01 06:24'><quiz q_id='1'>
<q_name>Airplane!</q_name >
<q_appleid>com.patelware.moviesquiz.free</q_appleid>
<q_description>"Roger, Roger"</q_description>
<q_urlicon>http://www.inall3.com/king/icons/airplane_icon.png</q_urlicon>
<q_urlmainimg>http://www.inall3.com/king/icons/airplane_main.png</q_urlmainimg>
<q_urldb>http://www.inall3.com/king/dbs/airplane.xml</q_urldb>
<q_timesofupload>2011-05-01 00:00:00</q_timesofupload>
<q_priceindollar>0.00</q_priceindollar>
<q_cattype>Comedy</q_cattype>
<q_thighscore>http://www.inall3.com/hs/highscoreairplane.php?action=gethighscores&mode=1</q_thighscore>
<q_phighscore>http://www.inall3.com/hs/highscoreairplane.php?action=gethighscores&mode=2</q_phighscore>
</quiz>
Here is my parsing code:
public class DynamicXmlHandler extends DefaultHandler {
DynamicXMLgettersetter info = new DynamicXMLgettersetter();
boolean q_nameOn = false;
String q_nameValue = null;
boolean q_DescrptionOn = false;
String q_DescriptionValue = null;
boolean q_MainImgOn = false;
String q_MainImgValue = null;
boolean q_urldbOn = false;
String q_urldbValue = null;
boolean q_PriceOn = false;
String q_PriceValue = null;
boolean q_CatagoryOn = false;
String q_CatagoryValue = null;
boolean q_TmodeOn = false;
String q_TmodeValue = null;
boolean q_PmodeOn = false;
String q_PmodeValue = null;
// .................................................................
#Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("q_name")) {
q_nameOn = true;
}
if (localName.contentEquals("q_description")) {
q_DescrptionOn = true;
}
if (localName.contentEquals("q_urlmainimg")) {
q_MainImgOn = true;
}
if (localName.contentEquals("q_urldb")) {
q_urldbOn = true;
}
if (localName.contentEquals("q_priceindollar")) {
q_PriceOn = true;
}
if (localName.contentEquals("q_cattype")) {
q_CatagoryOn = true;
}
if (localName.contentEquals("q_thighscore")) {
q_TmodeOn = true;
}
if (localName.contentEquals("q_phighscore")) {
q_PmodeOn = true;
}
super.startElement(uri, localName, qName, attributes);
}
// ..................................................................
#Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (q_nameOn == true) {
q_nameValue = new String(ch, start, length);
}
if (q_DescrptionOn == true) {
q_DescriptionValue = new String(ch, start, length);
}
if (q_MainImgOn == true) {
q_MainImgValue = new String(ch, start, length);
}
if (q_urldbOn == true) {
q_urldbValue = new String(ch, start, length);
}
if (q_PriceOn == true) {
q_PriceValue = new String(ch, start, length);
}
if (q_CatagoryOn == true) {
q_CatagoryValue = new String(ch, start, length);
}
if (q_TmodeOn == true) {
q_TmodeValue = new String(ch, start, length);
}
if (q_PmodeOn == true) {
q_PmodeValue = new String(ch, start, length);
}
super.characters(ch, start, length);
}
// ..................................................................
#Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("q_name")) {
q_nameOn = false;
info.setqname(q_nameValue);
}
if (localName.equalsIgnoreCase("q_description")) {
q_DescrptionOn = false;
info.setdescription(q_DescriptionValue);
}
if (localName.equalsIgnoreCase("q_urlmainimg")) {
q_MainImgOn = false;
info.seturlimage(q_MainImgValue);
}
if (localName.equalsIgnoreCase("q_urldb")) {
q_urldbOn = false;
info.seturldb(q_urldbValue);
}
if (localName.equalsIgnoreCase("q_priceindollar")) {
q_PriceOn = false;
info.setpriceindollar(q_PriceValue);
}
if (localName.equalsIgnoreCase("q_cattype")) {
q_CatagoryOn = false;
info.setcattype(q_CatagoryValue);
}
if (localName.equalsIgnoreCase("q_thighscore")) {
q_TmodeOn = false;
info.setthighscore(q_TmodeValue);
}
if (localName.equalsIgnoreCase("q_phighscore")) {
q_PmodeOn = false;
info.setphighscore(q_PmodeValue);
}
super.endElement(uri, localName, qName);
}
When I parse it using SAX parser everything is returned correctly. But when I try to parse <q_thighscore> or <q_phighscore>, it's returning just mode=1 and mode=2, but I want full link to be parsed, where is the problem and what should I do now?
those two entities have an ampersand - a "&" - that is being turned into a " & # 3 8 ;"
so you've got to deal with those in your code differently

XML response how to assign values to variables

I get the xml repsonse for http request. I store it as a string variable
String str = in.readLine();
And the contents of str is:
<response>
<lastUpdate>2012-04-26 21:29:18</lastUpdate>
<state>tx</state>
<population>
<li>
<timeWindow>DAYS7</timeWindow>
<confidenceInterval>
<high>15</high>
<low>0</low>
</confidenceInterval>
<size>0</size>
</li>
</population>
</response>
I want to assign tx, DAYS7 to variables. How do I do that?
Thanks
Slightly modified code from http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
public class ReadXMLFile {
// Your variables
static String state;
static String timeWindow;
public static void main(String argv[]) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
// Http Response you get
String httpResponse = "<response><lastUpdate>2012-04-26 21:29:18</lastUpdate><state>tx</state><population><li><timeWindow>DAYS7</timeWindow><confidenceInterval><high>15</high><low>0</low></confidenceInterval><size>0</size></li></population></response>";
DefaultHandler handler = new DefaultHandler() {
boolean bstate = false;
boolean tw = false;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("STATE")) {
bstate = true;
}
if (qName.equalsIgnoreCase("TIMEWINDOW")) {
tw = true;
}
}
public void characters(char ch[], int start, int length) throws SAXException {
if (bstate) {
state = new String(ch, start, length);
bstate = false;
}
if (tw) {
timeWindow = new String(ch, start, length);
tw = false;
}
}
};
saxParser.parse(new InputSource(new ByteArrayInputStream(httpResponse.getBytes("utf-8"))), handler);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("State is " + state);
System.out.println("Time windows is " + timeWindow);
}
}
If you're running this as a part of some process you might want to extend the ReadXMLFile from DefaultHandler.

Categories