0

I have done a sax parser that parses a xml file and prints the tags on the console.

The problem is that they don't follow a hierarchy.

Look at this:

-------------------<GOT>
-------------------<character>
-------------------<id>
-------------------<name>
----------------------->Arya Stark
-------------------<gender>
----------------------->Female
-------------------<culture>
----------------------->Northmen
-------------------<born>
----------------------->In 289 AC, at Winterfell
-------------------<died>
-------------------<alive>
----------------------->TRUE
-------------------<titles>
-------------------<title>
----------------------->Princess

For example, character and id are on the same level. Any idead on how to detect if a tag is a child of another?

Thanks!

public class Sax extends DefaultHandler {
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
                            System.out.println("-------------------<" + qName + ">");

          }
      
   
    public void characters(char ch[], int start, int length)
            throws SAXException {
            
            
            if( new String(ch,start,length).matches(".*[a-zA-Z0-9]+.*")){
                    System.out.println("----------------------->" + new String(ch, start, length));

        } else {
        } 
            
            
      
        }
    
        public void endElement(String uri, String localName, String qName)
            throws SAXException {
 
        System.out.println("</" + qName + ">");
 
    
    }
}

This is the code of the sax parser, I need to know a way to detect if a tag has a child.

I am currently reading about sax parser, so if I find out I will post it!

package sax;

 
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
 
public class ParseXMLFileSax {
 
    private static final String xmlFilePath = "got.xml";
 
    public static void main(String argv[]) {
 
        try {
 
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
 
            saxParser.parse(xmlFilePath, new Sax());
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
}

This class does the parser and calls newSaxParser class.

4
  • If you won't show us your code, how do you expect us to deal with this? Commented Oct 16, 2020 at 14:48
  • You are right, sorry Commented Oct 16, 2020 at 14:53
  • This doesn't look like you've either written a parser or are using one. Commented Oct 16, 2020 at 15:00
  • Updated the code, forgot to add the actuall parser, my bad Commented Oct 16, 2020 at 15:02

1 Answer 1

0

SAX just a stream of events, so you should somehow maintain handler state to implement your desired logic. E.g. here there is a bunch of boolean flags

How can I parse nested elements in SAX Parser in java?

In your question is not clear what's exactly your goal. If you just want to indent tags in output, you could have a integer variable for indentation, so you could increment it on element start and decrement it on element end.

Try to find some tutorial and follow it, e.g. here https://www.informit.com/articles/article.aspx?p=26351&seqNum=5

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.