从xml解析创建对象

时间:2014-10-20 02:07:05

标签: java xml parsing

我是一名新程序员,我正在处理我正在进行的项目。我试图使用Saxparser解析xml文件而不是获取用户输入。我的xml文件有多种类型的“引用”,如书籍,期刊文章分机。我无法通过解析xml来填充对象层次结构,我有适当的类及其各自的构造函数以及getter和setter方法来创建对象。我唯一的问题是如何在解析时通过在我的主类中重写下面的方法来创建这些对象。

      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");

下面是我试图解析的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>

1 个答案:

答案 0 :(得分:0)

在startElement事件处启动对象的内部数据模型,该事件表示对象。在程序中的变量/数据结构中累积对象的数据。当您到达该对象的endElement时,请使用您存储的数据来构造/配置对象。

如果对象可以嵌套,则可能需要维护上下文堆栈。如果它们以其他方式相关,您可能必须将ID和IDREF映射到对这些对象的引用。

可以在网上找到许多例子。存在几种相互竞争的半标准表示形式,以及一大堆自定义方法。

相关问题