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> 
  • Do not make notes in the title like "not yet decided." Here it is not accepted. - Sergey Gornostaev

2 answers 2

 import java.io.IOException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SAXHandler extends DefaultHandler { private final String query; private String currentTagName; private Product currentProduct; private static class Product { private String id; private String name; private int price; public void setId(String id) { this.id = id; } public String getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setPrice(String price) { this.price = Integer.parseInt(price); } public int getPrice() { return price; } public String toString() { return String.format("Id: %s\nName: %s\nPrice: %d\n", id, name, price); } } public SAXHandler(String query) { this.query = query; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentTagName = qName; if(qName.equals("product")) { currentProduct = new Product(); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if(qName.equals("product")) { if (query.equals(currentProduct.getName())) System.out.println(currentProduct); currentProduct = null; } currentTagName = null; } @Override public void characters(char[] ch, int start, int length) throws SAXException { if(currentProduct == null) return; String tagContent = new String(ch, start, length).trim(); if ("id".equals(currentTagName)) currentProduct.setId(tagContent); if ("name".equals(currentTagName)) currentProduct.setName(tagContent); if ("price".equals(currentTagName)) currentProduct.setPrice(tagContent); } public static void main(final String args[]) { if (args.length < 1) { System.err.println("Введите имя искомого продукта"); return; } try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); SAXHandler handler = new SAXHandler(args[0]); saxParser.parse("test.xml", handler); } catch(SAXException ex) {} catch(ParserConfigurationException ex) {} catch(IOException ex) {} } } 
  • Thank you) but Well, this is just an example there should be so that it does not invoke any static names like id name and the program adequately responds to the change of all tags and the addition of new ones. That is a full-fledged parser - Vasil Chorny
  • Miracles do not happen. You will have to fully implement the state machine in the sax-processor code — each state and all transitions for a specific xml-document format and the required parsing logic. The format of the input or output data will change, you will have to rewrite the code. - Sergey Gornostaev
  • I do not where I can not find the information in the internet how can I create one link? - Vasil Chorny
  • The example in my answer shows how it can serve as a starting point for developing more complex parsers. Good SAX parsing is described in this tutorial . - Sergey Gornostaev

The only way to determine a parent when using SAX is to preserve the tag hierarchy while reading a document. Perhaps DOM parser is more suitable for your task (see DocumentBuilderFactory and DocumentBuilder ).

  • Unfortunately, I have a problem like this. And how can you preserve the hierarchy of tags in the process of reading a SAX document, please demonstrate if you are not too lazy) Or a link to an article. - Vasil Chorny