Примечание. Если это бесполезно, игнорируйте это.
Я обнаружил, что отвечаю на мой вопрос следующим образом: мы можем выделить (распечатать) все детали
мы можем сделать это с помощью парсера
1) DOM-парсер
2) SAX-парсер
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());
}
}
второй метод
public void ReadAndWriteXMLFileUsingSAXParser(){
try
{
DefaultHandler handler = new MyHandler();
// parseXmlFile("infilename.xml", handler, true);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
InputStream rStream = null;
rStream = getClass().getResourceAsStream("sample.xml");
saxParser.parse(rStream, handler);
}catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
class MyHandler extends DefaultHandler {
String rootname;Attributes atr;
private boolean flag=false;
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
rootname=localName;
atr=atts;
if(rootname.equalsIgnoreCase("ROW")){
flag=true;
int length = atts.getLength();
for (int i=0; i<length; i++) {
String name = atts.getQName(i);
String value = atts.getValue(i);
System.out.println(name+"**********"+value);
}
}
}
public void characters(char[] ch, int start, int length){
String value=new String(ch,start,length);
if(flag)
{
if(rootname.equalsIgnoreCase("COL")){
int length2 = atr.getLength();
for (int i=0; i<length2; i++) {
String name1 = atr.getQName(i);
String value1 = atr.getValue(i);
System.out.println(name1+"**********"+value1);
}
System.out.println("++++++++++++++"+value);
}
}
}
public void endElement(String uri, String localName, String qName){
rootname=localName;
if(rootname.equalsIgnoreCase("ROW")){
flag=false;
}
}
}
мы получили вывод, когда DOM-парсер
Root element of the doc is ROOT
Total no of people : 3
==============Name value = Product Name~B
===============CheckBox value =
==============Type value = PerformanceNormal
==============Count value = 1
==============ChildCount value = 1
==============Show value = Y
==============Refine value = B
==============Page value = 1
==============Image value =
==============ProductID value = 3
+++++++++++++++Node Value : Sainsbury's aromatherapy citrus mint
==============Page value = 1
==============Image value =
==============ProductID value = 4
+++++++++++++++Node Value : TestPerf
==============Page value = 1
==============Image value =
==============ProductID value = 5
+++++++++++++++Node Value : TestPerf001
==============Page value = 1
==============Image value =
==============ProductID value = 24
+++++++++++++++Node Value : Ashraf Product
==============Page value = 2
==============Image value =
==============ProductID value = 25
+++++++++++++++Node Value : Acb
==============Name value = Region~H
===============CheckBox value =
==============Type value = PerformanceNormal
==============Count value = 2
==============ChildCount value = 1
==============Show value = Y
==============Refine value = H
==============Page value = 1
==============Image value =
==============ProductID value = 3
+++++++++++++++Node Value : Western Europe
==============Page value = 1
==============Image value =
==============ProductID value = 4
+++++++++++++++Node Value : Western Europe
==============Page value = 1
==============Image value =
==============ProductID value = 5
+++++++++++++++Node Value : Western Europe
==============Page value = 1
==============Image value =
==============ProductID value = 24
+++++++++++++++Node Value : Central and Eastern Europe
==============Page value = 2
==============Image value =
==============ProductID value = 25
+++++++++++++++Node Value : Central and Eastern Europe
==============Name value = Country~H
===============CheckBox value =
==============Type value = PerformanceNormal
==============Count value = 3
==============ChildCount value = 1
==============Show value = Y
==============Refine value = H
==============Page value = 1
==============Image value =
==============ProductID value = 3
+++++++++++++++Node Value : United Kingdom
==============Page value = 1
==============Image value =
==============ProductID value = 4
+++++++++++++++Node Value : Belgium
==============Page value = 1
==============Image value =
==============ProductID value = 5
+++++++++++++++Node Value : Belgium
==============Page value = 1
==============Image value =
==============ProductID value = 24
+++++++++++++++Node Value : Czech Republic
==============Page value = 2
==============Image value =
==============ProductID value = 25
+++++++++++++++Node Value : Czech Republic
как при SAXParsing
Name**********Product Name~B
CheckBox**********
Type**********PerformanceNormal
Count**********1
ChildCount**********1
Show**********Y
Refine**********B
Page**********1
Image**********
ProductID**********3
++++++++++++++Sainsbury's aromatherapy citrus mint
Page**********1
Image**********
ProductID**********4
++++++++++++++TestPerf
Page**********1
Image**********
ProductID**********5
++++++++++++++TestPerf001
Page**********1
Image**********
ProductID**********24
++++++++++++++Ashraf Product
Page**********2
Image**********
ProductID**********25
++++++++++++++Acb
Name**********Region~H
CheckBox**********
Type**********PerformanceNormal
Count**********2
ChildCount**********1
Show**********Y
Refine**********H
Page**********1
Image**********
ProductID**********3
++++++++++++++Western Europe
Page**********1
Image**********
ProductID**********4
++++++++++++++Western Europe
Page**********1
Image**********
ProductID**********5
++++++++++++++Western Europe
Page**********1
Image**********
ProductID**********24
++++++++++++++Central and Eastern Europe
Page**********2
Image**********
ProductID**********25
++++++++++++++Central and Eastern Europe
Name**********Country~H
CheckBox**********
Type**********PerformanceNormal
Count**********3
ChildCount**********1
Show**********Y
Refine**********H
Page**********1
Image**********
ProductID**********3
++++++++++++++United Kingdom
Page**********1
Image**********
ProductID**********4
++++++++++++++Belgium
Page**********1
Image**********
ProductID**********5
++++++++++++++Belgium
Page**********1
Image**********
ProductID**********24
++++++++++++++Czech Republic
Page**********2
Image**********
ProductID**********25
++++++++++++++Czech Republic