How can the text of the tags that are inside the father's tag tell which father he belongs to and give the whole value inside the father using one of these values using the SAX method?
The DOM method and XPATH have already been processed, I need help with SAX.
Well, roughly speaking, the user must enter Product1 , and he should be Product1 : Product1 belongs to the id tag inside the product tag and should have a list of the value of this tag ("Id", "Name", "Price") + its value, and as if to completely browse all that is inside the product and issue. Well, this is just an example there should be so that it does not invoke any static names, but the program adequately responds to the change of all tags and the addition of new ones. Implemented already search by attributes:
DefaultHandler handler=new DefaultHandler(){ public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException { super.startElement(uri, localName, qName, attributes); String name= attributes.getValue(textField.getText()); if(name!=null && !name.isEmpty()){ System.out.println(name); } } public void characters(char[] ch, int start, int length) throws SAXException{ String str=""; for(int i=0; i<length; i++){ str+=ch[start+i]; }if(textField.getText().equals("all")){ System.out.println(str);} } }; SAXParserFactory factory= SAXParserFactory.newInstance(); SAXParser parser=factory.newSAXParser(); parser.parse(new File("src\\data\\products.xml"), handler); Data:
<products> <product name="eee" price="122"> <id>p01</id> <name>Product1</name> <price>100</price> </product> <product name="pp" price="123"> <id>p02</id> <name>Product2</name> <price>200</price> </product> <product name="rr" price="124"> <id>p03</id> <name>Product3</name> <price>300</price> </product> <product name="rrr" price="125"> <id>p04</id> <name>Product4</name> <price>400</price> </product>