XML Tokenizer Question

T

Tony

Hello,
I'm trying to build a simple Tokenzier For XML:
I have the following String tat I get from the serverand want to build
a method that takes "property name" and returns it's value.

for example:
<dictionary>
<Status>1</Status>
<Reason>0</Reason>
<Key>JFKDL54GFDYU85T43NJKGH89N543KGFD</Key>
</dictionary>

if I call GetVaule("Key"); I want to get back as string
"JFKDL54GFDYU85T43NJKGH89N543KGFD".

I tried to use Kxml parser but it has a problem with unicode / UTF-8
coding. (especailly with hebrew and arabic)

same thing about Nanoxml
note that my XML file is very simple and small (it won't be more than
5-6 properties)

any help ?
 
O

Oliver Wong

Tony said:
Hello,
I'm trying to build a simple Tokenzier For XML:
I have the following String tat I get from the serverand want to build
a method that takes "property name" and returns it's value.

for example:
<dictionary>
<Status>1</Status>
<Reason>0</Reason>
<Key>JFKDL54GFDYU85T43NJKGH89N543KGFD</Key>
</dictionary>

if I call GetVaule("Key"); I want to get back as string
"JFKDL54GFDYU85T43NJKGH89N543KGFD".

I tried to use Kxml parser but it has a problem with unicode / UTF-8
coding. (especailly with hebrew and arabic)

same thing about Nanoxml
note that my XML file is very simple and small (it won't be more than
5-6 properties)

any help ?

Is the keyword "Tokenizer" important? From your description of the
problem, it doesn't sounds like you want a tokenizer at all.

Did you try using Java's DOM or SAX API and just accepting whatever
default XML parser it uses?

- Oliver
 
?

=?iso-8859-1?q?Fl=E1vio_Barata?=

Try this Java 1.5 code:

public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory1 =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder1 = factory1.newDocumentBuilder();
Document doc1 = builder1.parse(new File("yourFileToBeParsed.xml"));
System.out.println("> " +
getText(doc1.getElementsByTagName("Key").item(0)));
}

private static String getText(Node node) {
StringBuilder sb = new StringBuilder();
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node n = nodeList.item(i);
if (n.getNodeType() == Node.TEXT_NODE) {
sb.append(n.getNodeValue().trim());
}
}
return sb.toString();
}

-
Flavio Santos
 

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
474,265
Messages
2,571,071
Members
48,771
Latest member
ElysaD

Latest Threads

Top