escaped XML data writing/reading/parsing queries.

P

Piper707

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.
 
H

Hiran Chaudhuri

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

What you mention here is correct. Now how do you parse this xml document?
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?

It seems your XML document is correct, and your parser does the right thing
to change the entity reference into the real character. In java you compare
the two strings (operator.equals("<")) and this is successful.

Now your problem might arise if the List.indexOf(...) method does not do a
string compare but a direct compare ( object1 == object2). If object1 is the
same instance as object2 the expression is true, but if you have two
instances the expression is false, even if the objects have the equal
content.

Hiran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top