Hi,
I am using the following code to parse a XML doc.
******
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParse
rFactory;
import javax.xml.parsers.ParserCo
nfiguratio
nException
;
import javax.xml.parsers.SAXParse
r;
public class BooksLibrary extends HandlerBase
{
protected static final String XML_FILE_NAME = "E:\\Properties File\\Sample.xml";
public static void main (String argv [])
{
// Use the default (non-validating) parser
// SAXParserFactory factory = SAXParserFactory.newInstan
ce();
//factory.setValidating(tr
ue);
SAXParserFactory factory = SAXParserFactory.newInstan
ce();
try {
// Set up output stream
out = new OutputStreamWriter (System.out, "UTF8");
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(XML_FILE_NAME), new BooksLibrary() );
} catch (Throwable t) {
t.printStackTrace ();
}
System.exit (0);
}
static private Writer out;
//========================
==========
==========
==========
=====
// Methods in SAX DocumentHandler
//========================
==========
==========
==========
=====
public void startDocument ()
throws SAXException
{
showData ("<?xml version='1.0' encoding='UTF-8'?>");
newLine();
}
public void endDocument ()
throws SAXException
{
try {
newLine();
out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
public void startElement (String name, AttributeList attrs)
throws SAXException
{
showData ("<"+name);
if (attrs != null) {
for (int i = 0; i < attrs.getLength (); i++) {
showData (" ");
showData (attrs.getName(i)+"=\""+at
trs.getVal
ue (i)+"\"");
}
}
showData (">");
}
public void endElement (String name)
throws SAXException
{
showData ("</"+name+">");
}
public void characters (char buf [], int offset, int len)
throws SAXException
{
String s = new String(buf, offset, len);
showData (s);
}
//========================
==========
==========
==========
=====
// Helpers Methods
//========================
==========
==========
==========
=====
// Wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
private void showData (String s)
throws SAXException
{
try {
out.write (s);
out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
// Start a new line
private void newLine ()
throws SAXException
{
String lineEnd = System.getProperty("line.s
eparator")
;
try {
out.write (lineEnd);
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
}
*******
My XML file look like this
******
<?xml version="1.0" encoding="UTF-8"?>
<Body>
<Response>
<ActCd>1000</ActCd>
<RtrnCd>123</RtrnCd>
<GlobalUserId>123213</Glob
alUserId>
<FirstNm>John</FirstNm>
<LastNm>Manathon</LastNm>
<MiddleNm>Willy</MiddleNm>
<ElectronicAddrTxt>Somethi
ng here</ElectronicAddrTxt>
<Userid>test</Userid>
<TransAcctNbr>234234234324
</TransAcc
tNbr>
</Response>
</Body>
******
This code is returning me this same XML. I need the out put in different form:
I need the out in the following form:
ActCd :- 1000
FirstNm: John
----
----
---
Like this all the elements of the XML. Do you have any idea what needs to be changed to get this output.
Thanks in advance
XMEA
Start Free Trial