Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

escapd XML writing/reading/parsing queries

Thread view: 
Piper707@hotmail.com - 12 Dec 2005 05:24 GMT
Hi,

My <operator> tag in XML can take any of the following values:

"<", ">", and "<>"

I enter the data this way in the xml:

<operator>&lt;</operator>
<operator>&gt;</operator>

This works fine for the first two.

1) How do I enter "<>" as data in the xml document?  I tried
<operator>&gt;&lt;</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>&lt;</OPERATOR>

This is the method
------------------

List operatorList = Arrays.asList("<>", "&lt;&gt;", "&lt;" ,"<", "&gt;"
,">");

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>&lt;&gt;</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>
&lt;&gt;
</OPERATOR>
</TEST>

I'm expecting &gt;&lt; 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(">", "&gt;", "<", "&lt;", "<>",
"&lt;&gt;");

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.