Question Regarding XML Parsing Using SAX in JAVA

2019-08-08 20:36发布

问题:

I am trying to parse an XML document in Java (first time attempting to do so), and I have found a number of articles on Google that provide examples but I really don't understand some of the code in them, so I was wondering if someone could explain a few things to me. The primary example I was looking at came from here: http://www.java-tips.org/java-se-tips/org.xml.sax/parsing-xml-with-a-simple-sax-document-handler-2.html.

My main questions primarily come from the second half of the code, specifically the part of...

 // we enter to element 'qName':
        public void startElement(String uri, String localName, 
                String qName, Attributes attrs) throws SAXException {

            if (qName.equals("purchase-order")) {
            } else if (qName.equals("date")) {
            } /* if (...)
                        } */ else {
                throw new IllegalArgumentException("Element '" + 
                        qName + "' is not allowed here");
                        }
        }

        // we leave element 'qName' without any actions:
        public void endElement(String uri, String localName, String qName)
        throws SAXException {
            // do nothing;
        }

...could someone explain to me what exactly "String uri, String localName, String qName, and Attributes attrs are? I assume they are not code specific as I've seen them in a few examples. Also (assuming) that these are some sort of root elements in the XML file, could anyone provide some direction as to how I could use these to parse through the "document.xml.rels" file located within a .docx directory in a way that it would read and accept only the image files (not header, settings, etc) and their associated Relationship Id #'s? (Below is the short document I'm referring to)...

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
      <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml" /> 
      <Relationship Id="rId13" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" /> 
      <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml" /> 
      <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Target="header1.xml" /> 
      <Relationship Id="rId12" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml" /> 
      <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" /> 
      <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml" /> 
      <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml" /> 
      <Relationship Id="rId11" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image3.png" /> 
      <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml" /> 
      <Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image2.jpeg" /> 
      <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml" /> 
      <Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpeg" /> 
</Relationships>

Thanks for any help in advance!

回答1:

Look at the javadoc of SAX ContentHandler.startElement(..)



回答2:

URI :   Namespace URI
LOCAL NAME : the local name (without prefix)
QName : the qualified XML name (with prefix)