The sample XML file is:
sample.xml:
<ROOT>
<ROW Name="Product Name~B" CheckBox="" Type="PerformanceNormal" Count="1" ChildCount="1" Show="Y" Refine="B">
<COL Page="1" Image="" ProductID="3">Sainsbury's aromatherapy citrus mint</COL>
<COL Page="1" Image="" ProductID="4">TestPerf</COL>
<COL Page="2" Image="" ProductID="25">Acb</COL>
</ROW>
<ROW Name="Region~H" CheckBox="" Type="PerformanceNormal" Count="2" ChildCount="1" Show="Y" Refine="H">
<COL Page="1" Image="" ProductID="3">Western Europe</COL>
<COL Page="1" Image="" ProductID="5">Western Europe</COL>
<COL Page="1" Image="" ProductID="24">Central and Eastern Europe</COL>
</ROW>
</ROOT>
--------------------------------
and parse this xml file like:
public class XMLFileParsingScreen extends MainScreen implements FieldChangeListener
{
private ButtonField domparser;
public XMLFileParsingScreen() {
createGUI();
}
private void createGUI(){
domparser=new ButtonField("DOM Parser");
domparser.setChangeListener(this);
add(domparser);
}
public void fieldChanged(Field field, int context)
{
if(field==domparser)
{
ReadAndPrintXMLfileUsingDOM();
}
}
public void ReadAndPrintXMLfileUsingDOM()
{
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
InputStream rStream = null;
try
{
rStream = getClass().getResourceAsStream("sample.xml");
}catch (Exception e)
{
System.out.println(e.getMessage());
}
Document doc = docBuilder.parse(rStream);
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("ROW");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++)
{
Node firstPersonNode = listOfPersons.item(s);
Element firstPersonElement = (Element)firstPersonNode;
System.out.println( "==============Name value = " + firstPersonElement.getAttribute( "Name" ) );
System.out.println( "===============CheckBox value = " + firstPersonElement.getAttribute( "CheckBox" ));
System.out.println( "==============Type value = " + firstPersonElement.getAttribute( "Type" ));
System.out.println( "==============Count value = " + firstPersonElement.getAttribute( "Count" ));
System.out.println( "==============ChildCount value = " + firstPersonElement.getAttribute( "ChildCount" ));
System.out.println( "==============Show value = " + firstPersonElement.getAttribute( "Show" ));
System.out.println( "==============Refine value = " + firstPersonElement.getAttribute( "Refine" ));
NodeList firstNameList = firstPersonElement.getElementsByTagName("COL");
int child_lng=firstNameList.getLength();
for(int j=0;j<child_lng;j++)
{
Node innernode=firstNameList.item(j);
Element firstPersonElement1 = (Element)innernode;
// NamedNodeMap attributes = innernode.getAttributes();
Node value=firstNameList.item(j).getChildNodes().item(0);
System.out.println( "==============Page value = " + firstPersonElement1.getAttribute( "Page" ) );
System.out.println( "==============Image value = " + firstPersonElement1.getAttribute( "Image" ) );
System.out.println( "==============ProductID value = " + firstPersonElement1.getAttribute( "ProductID" ) );
System.out.println("+++++++++++++++Node Value : " +value.getNodeValue());
}
}
}catch (SAXParseException err)
{
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e)
{
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Exception t)
{
System.out.println(t.getMessage());
}
}
}
any doubts feel free to mail on alishaik001@gmail.com
sample.xml:
<ROOT>
<ROW Name="Product Name~B" CheckBox="" Type="PerformanceNormal" Count="1" ChildCount="1" Show="Y" Refine="B">
<COL Page="1" Image="" ProductID="3">Sainsbury's aromatherapy citrus mint</COL>
<COL Page="1" Image="" ProductID="4">TestPerf</COL>
<COL Page="2" Image="" ProductID="25">Acb</COL>
</ROW>
<ROW Name="Region~H" CheckBox="" Type="PerformanceNormal" Count="2" ChildCount="1" Show="Y" Refine="H">
<COL Page="1" Image="" ProductID="3">Western Europe</COL>
<COL Page="1" Image="" ProductID="5">Western Europe</COL>
<COL Page="1" Image="" ProductID="24">Central and Eastern Europe</COL>
</ROW>
</ROOT>
--------------------------------
and parse this xml file like:
XMLFileParsingScreen.java :
public class XMLFileParsingScreen extends MainScreen implements FieldChangeListener
{
private ButtonField domparser;
public XMLFileParsingScreen() {
createGUI();
}
private void createGUI(){
domparser=new ButtonField("DOM Parser");
domparser.setChangeListener(this);
add(domparser);
}
public void fieldChanged(Field field, int context)
{
if(field==domparser)
{
ReadAndPrintXMLfileUsingDOM();
}
}
public void ReadAndPrintXMLfileUsingDOM()
{
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
InputStream rStream = null;
try
{
rStream = getClass().getResourceAsStream("sample.xml");
}catch (Exception e)
{
System.out.println(e.getMessage());
}
Document doc = docBuilder.parse(rStream);
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("ROW");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++)
{
Node firstPersonNode = listOfPersons.item(s);
Element firstPersonElement = (Element)firstPersonNode;
System.out.println( "==============Name value = " + firstPersonElement.getAttribute( "Name" ) );
System.out.println( "===============CheckBox value = " + firstPersonElement.getAttribute( "CheckBox" ));
System.out.println( "==============Type value = " + firstPersonElement.getAttribute( "Type" ));
System.out.println( "==============Count value = " + firstPersonElement.getAttribute( "Count" ));
System.out.println( "==============ChildCount value = " + firstPersonElement.getAttribute( "ChildCount" ));
System.out.println( "==============Show value = " + firstPersonElement.getAttribute( "Show" ));
System.out.println( "==============Refine value = " + firstPersonElement.getAttribute( "Refine" ));
NodeList firstNameList = firstPersonElement.getElementsByTagName("COL");
int child_lng=firstNameList.getLength();
for(int j=0;j<child_lng;j++)
{
Node innernode=firstNameList.item(j);
Element firstPersonElement1 = (Element)innernode;
// NamedNodeMap attributes = innernode.getAttributes();
Node value=firstNameList.item(j).getChildNodes().item(0);
System.out.println( "==============Page value = " + firstPersonElement1.getAttribute( "Page" ) );
System.out.println( "==============Image value = " + firstPersonElement1.getAttribute( "Image" ) );
System.out.println( "==============ProductID value = " + firstPersonElement1.getAttribute( "ProductID" ) );
System.out.println("+++++++++++++++Node Value : " +value.getNodeValue());
}
}
}catch (SAXParseException err)
{
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e)
{
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Exception t)
{
System.out.println(t.getMessage());
}
}
}
any doubts feel free to mail on alishaik001@gmail.com
No comments:
Post a Comment