简体   繁体   English

通过xml解析创建对象

[英]creating objects from xml parsing

I am a new programmer and I am having trouble with a project I am working on. 我是一名新程序员,我在从事的项目遇到麻烦。 I am trying to parse a xml file using a Saxparser instead of getting user input. 我正在尝试使用Saxparser解析xml文件,而不是获取用户输入。 My xml file has multiple types of "citations" such as books, journal articles ext. 我的xml文件具有多种类型的“引文”,例如书籍,期刊文章等。 I am having trouble filling an object hierarchy by parsing an xml, I have the appropriate classes with their respective constructers and getter and setter methods to create the objects. 我无法通过解析xml来填充对象层次结构,我拥有适当的类及其各自的构造方法以及getter和setter方法来创建对象。 My only question is how to create these objects while parsing by overriding these methods below in my main class. 我唯一的问题是如何在解析时通过覆盖主类下面的这些方法来创建这些对象。

      public class main extends DefaultHandler{

          @Override
           public void startElement(String s, String s1, String tagname, Attributes attr ) throws SAXException{
           if(tagname.equals("Book")){
            }

           }

          @Override
          public void characters(char [] ac, int i, int s)throws SAXException{

          }

          @Override 
          public void endElement(String s, String s1, String tag)throws SAXException{

          }


           public static void main(String[] args) throws IOException,      ParserConfigurationException, SAXException {
           // Create scanner
           Scanner OswegoNote = new Scanner(System.in);
          //Create a parser factory
           SAXParserFactory factory = SAXParserFactory.newInstance();
           //make the parser
           SAXParser saxParser = factory.newSAXParser();
           XMLReader parser = saxParser.getXMLReader();
           //create a handler
           main handler = new main();
           //tell the parser to use the handler
          parser.setContentHandler(handler);
          //Read and parse the document
          parser.parse("xmlFile.xml");

Below is part of the xml file I am attempting to parse and retrieve the shown values for each citation type. 以下是我试图解析和检索每种引用类型的显示值的xml文件的一部分。

        <Citation>
              <Book>
                <Name>A Wavelet Tour of Signal Processing</Name>
                <Publisher>Academic Press</Publisher>
                <PublicationDate>01/01/2009</PublicationDate>
                <PublicationPlace>Burlington,MA</PublicationPlace>
              <Authors>
                <author>Stephanie M Mallot</author>
                </Authors>
              <Keywords>
                <Keyword>DSP</Keyword>
                <Keyword>Wavelets</Keyword>
              <Keyword>Sparse Data</Keyword>
              </Keywords>
            </Book>
        </Citation>


        <Citation>
           <JournalArticle>
              <Name>The attractions of stupidity</Name>
              <TitleOfJournal>The St. Croix Review</TitleOfJournal>
              <PublicationDate>October, 2002</PublicationDate>
              <volNumber>30</volNumber>
              <IssueNumber>2</IssueNumber>
            <Authors>
               <author>Harry Hank Hirsh</author>
               <author>Mark Harold Coen</author>
               <author>Michael Charles Mozer</author>
           </Authors>
               <Pages StartPage="6" EndPage="10"/>
           <Keywords>
               <Keyword>Psychology</Keyword>
            <Keyword>Sociology</Keyword>
            <Keyword>Intelligence</Keyword>
            <Keyword>Sexuality</Keyword>
           </Keywords>
         </JournalArticle>
       </Citation>

Start an internal data model for the object at the startElement event which represents the object. 在代表该对象的startElement事件中为该对象启动内部数据模型。 Accumulate data for the object in variables/data-structures in your program. 在程序的变量/数据结构中累积对象的数据。 When you reach the endElement for that object, use the data you've stored to construct/configure the object. 当到达该对象的endElement时,请使用存储的数据来构造/配置该对象。

If the objects can be nested, you may need to maintain a context stack. 如果对象可以嵌套,则可能需要维护上下文堆栈。 If they're related in other ways, you may have to map IDs and IDREFs to references to those objects. 如果它们以其他方式关联,则可能必须将ID和IDREF映射到对这些对象的引用。

Many examples can be found on the web. 在网上可以找到很多例子。 Several competing semi-standard representations exist, plus a whole bunch of custom approaches. 存在几种竞争的半标准表示形式,以及一堆定制方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM