Hi,
My <operator> tag in XML can take any of the following values:
"<", ">", and "<>"
I enter the data this way in the xml:
<operator><</operator>
<operator>></operator>
This works fine for the first two.
1) How do I enter "<>" as data in the xml document? I tried
<operator>><</operator>, but that only reads the first value,
i.e. "<".
2) After parsing the field, I need to match the data to the content of
an List, and return the matched index:
XML tag:
--------
<OPERATOR><</OPERATOR>
This is the method
------------------
List operatorList = Arrays.asList("<>", "<>", "<" ,"<", ">"
,">");
public void setOperator(String operator){
System.out.println("operator sent" + " ----> " + operator);
System.out.println("operator index" + " ----> " +
operatorList.indexOf(operator));
}
Method Output:
---------------
operator sent ----> <
operator index ----> -1
It is unable to find either "<" or ">" in my list, I can't understand
why?
3) However when I try this:
if(operator.equals("<"))
System.out.println("received <");
It prints out "received <" correctly.
Can someone please clarify?
Thank you,
Rohit.
Oliver Wong - 12 Dec 2005 23:06 GMT
> Hi,
>
[quoted text clipped - 49 lines]
>
> Can someone please clarify?
Can you post an SSCCE?
http://www.physci.org/codes/sscce.jsp
I'm not saying this just to be a pain, but there are so many places
where the error can be occuring, especially with respect to (1). Are you
escape too many times? Not enough times? Etc.
Try to post a small program and XML file such that the problem in (1) is
clearly demonstrated. E.g. the XML file should be something like
"<op><></op>" and the program shoud output to console "<". From there
we can see exactly what you're doing wrong.
- Oliver
Piper707@hotmail.com - 13 Dec 2005 04:35 GMT
Hi Oliver,
Strangely, part of the problem seems to have fixed itself. It works
okay for "<" and ">", but I'm still having trouble with the "<>" bit.
Try the foll program with this XML:
<TEST>
<OPERATOR>
<>
</OPERATOR>
</TEST>
I'm expecting >< to match up with index 4, rather it matches up
with 2, corresponding to "<"
-----------------------------------------------------------------------
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import java.io.*;
import java.util.Arrays;
import java.util.List;
public class MyExample{
public static void main(String[] args){
List operatorList = Arrays.asList(">", ">", "<", "<", "<>",
"<>");
try{
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader xmlEventReader =
xmlInputFactory.createXMLEventReader(new
FileInputStream("d:\\testing\\myexample.xml"));
while(xmlEventReader.hasNext())
{
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if(xmlEvent.isStartElement())
{
StartElement startElement = (StartElement)xmlEvent;
if(startElement.getName().toString().equals("OPERATOR"))
{
Characters charactersEvent = (Characters)
xmlEventReader.nextEvent();
String operator = charactersEvent.getData().trim();
System.out.println("operator sent ----> " +
operator);
System.out.println("index found ----> " +
operatorList.indexOf(operator));
}
}//if
}//while
}//try
catch(Exception e)
{
e.printStackTrace();
}
}//main
}//class
-----------------------------------------------------------------------
1) The parsing mtd is StaX, i wonder if the parser implementation could
be a problem?
2) Probably a silly question, I run programs using Eclipse, is it
possible for the JVM to maintain some sort of a "cache", which could
fudge my results? I ran this thing as part of my program about 10 times
yesterday and it wouldn't recognize "<" or ">" either. But today, I'm
only having trouble with <>!
Thanks
Rohit.