关于在JAVA中使用SAX进行XML解析的问题

时间:2011-06-27 16:09:38

标签: java xml sax xml-parsing docx

我正在尝试用Java解析XML文档(第一次尝试这样做),我在Google上发现了许多提供示例的文章,但我真的不理解其中的一些代码,所以我想知道是否有人可以向我解释一些事情。我看到的主要例子来自:http://www.java-tips.org/java-se-tips/org.xml.sax/parsing-xml-with-a-simple-sax-document-handler-2.html

我的主要问题主要来自代码的后半部分,特别是......的部分内容。

 // 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;
        }

...有人可以向我解释究竟是什么“字符串uri,字符串localName,字符串qName和属性attrs?我认为它们不是特定于代码的,因为我在一些例子中看过它们。另外(假设)这些是XML文件中的某种根元素,任何人都可以提供一些方向,告诉我如何使用它们以一种它将读取的方式解析位于.docx目录中的“document.xml.rels”文件并且只接受图像文件(不是标题,设置等)及其相关的关系ID#?(以下是我所指的简短文档)......

<?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>

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

查看SAX ContentHandler.startElement(..)

的javadoc

答案 1 :(得分:1)

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