Urgent--Help

I have a string ...say

String Content="<?xml version="1.0"?>

<page_data>

<template>home.htt</template>

<template_data></template_data>

<content>

<main><![CDATA[

><div><span class="Heading2">Welcome to Immediacy</span></div>

<div>Immediacy is an e-business software provider specialising in

content managment solutions. We aim:</div>

<ul type="disc">

<li class="MsoNormal">To provide world class content management

solutions that are within financial reach of the vast majority of

businesses</li>

<li class="MsoNormal">To enable organizations to communicate

effectively by empowering individuals with the knowledge to take

part</li>

<li class="MsoNormal"><span lang="EN-US">To provide customers with

a competitive advantage by accelerating publishing and lowering

production costs</span></li>

</ul>

<p class="MsoNormal">?</p>

<hr color="#66cc66" size="2" />

]]></main><HEAD><![CDATA[

><script language="Javascript">

// User editable scripts

function userinit()

{

// Any user edited initialisation here

}

</script>

<!--USER EDITS GO HERE-->

]]></HEAD><sidebar><![CDATA[><DIV>?</DIV>]]></sidebar></content>

</page_data>";

This value for String is an Xml content which I am getting from database...

Now I want to parse thru this String to get the node names and values...

Can Anybody help me...To approach the right way

[1854 byte] By [smitaunni] at [2007-9-26 2:28:45]
# 1

Hi!

IBM has a online tutorial about XML at http://www-105.ibm.com/developerworks/education.nsf/xml-onlinecourse-bytitle/090E4CED6DF2AA648525682B0052CBDA?OpenDocument

Somewhere in the "advanced" section it covers parsing a String. It is slightly outdated though.

Basically IBM's XML4J supports this kind of input, what gives you a fair chance that the derived Xerces parser will do the same.

mneumi at 2007-6-29 9:44:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

You could try this:

Lets say your string variable name is 'str'

byte[] b_arr = str.getBytes();

ByteArrayInputStream bstream = new ByteArrayInputStream(b_arr);

try{

SAXBuilder sbuilder = new SAXBuilder();

Document doc = sbuilder.build(bstream);

bstream.close();

}

catch(....

Now, you essentially have the Document and you can play around with it as you want!

But note that the Document object is a JDOM Doc, not the w3c Doc.

Hope this helps. For more clarity, you might want to have a look at the JDOM api.

arun78 at 2007-6-29 9:44:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Thanxs for ur suggestion....But I could find a bit more simple solution for it....I could solve the problem using StringReader.... Thanx anyway
smitaunni at 2007-6-29 9:44:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

> Now I want to parse thru this String to get the node

> names and values...

>

> Can Anybody help me...To approach the right way

I think that you may be looking at this in the wrong way. A better use of XML is to apply a stylesheet to it to get your HTML look and feel for your data. This involves no parsing of strings at all. It's actually very cool what you can do with XML stylesheets and JavaScript. If you want you can also use stylesheets to create server side HTML and transfer that to the client.

dubwai at 2007-6-29 9:44:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

After reading your question again, I realize I answered a different question.

The response that said to use the DOM is right. It is not hard to use, it just has a bit of a learning curve. You could write a whole set of classes to parse the string but they are already built. Go to http://java.sun.com/xml/jaxp-1.0.1/docs/api/index.html

for one solution.

dubwai at 2007-6-29 9:44:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...