关于在JAVA中使用SAX进行XML解析的问题 [英] Question Regarding XML Parsing Using SAX in JAVA

查看:55
本文介绍了关于在JAVA中使用SAX进行XML解析的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java解析XML文档(第一次尝试这样做),并且我在Google上找到了许多提供示例的文章,但是我真的不理解其中的某些代码,因此我想知道是否有人可以向我解释一些事情.我正在查看的主要示例来自此处:

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

...有人可以向我解释字符串uri,字符串localName,字符串qName和属性attrs是什么吗?我假设它们不是特定于代码的,正如我在一些示例中所看到的那样."(假设)这些是XML文件中的某种根元素,任何人都可以就我如何使用它们来解析位于.docx目录中的"document.xml.rels"文件中的方式提供某种指导,以其读取的方式并仅接受图像文件(不包括标题,设置等)及其关联的关系ID号(以下是我要引用的简短文档)...

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

感谢您的任何帮助!

推荐答案

查看SAX的Javadoc

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

这篇关于关于在JAVA中使用SAX进行XML解析的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆