用Kotlin在Spring Boot中读取超大StackOverflow XML

举报
Jet Ding 发表于 2020/09/30 16:47:31 2020/09/30
【摘要】 编程语言:kotlin框架:Spring Boot测试文件大小:73G private fun loadXML() { val factory = SAXParserFactory.newInstance() try { val parser = factory.newSAXParser() val file = F...

编程语言:
kotlin

框架:
Spring Boot

测试文件大小:
73G

  private fun loadXML() {
        val factory = SAXParserFactory.newInstance()
        try {

            val parser = factory.newSAXParser() 
            val file = File("D:/data/stackoverflow/stackoverflow.com-Posts/PostsCopy.xml") 
            val rowHandler = RowHandler() 
            parser.parse(file, rowHandler) 
        } catch (eParserConfigurationException) { 
            e.printStackTrace() 
        } catch (eIOException) { 
            e.printStackTrace() 
        } catch (eSAXException) { 
            e.printStackTrace() 
        } 

    } 
import com.ding.xml.models.Row 
import org.xml.sax.Attributes 
import org.xml.sax.SAXException 
import org.xml.sax.helpers.DefaultHandler 

class RowHandler : DefaultHandler() { 

    @Throws(SAXException::class) 
    override fun startElement(uriString?localNameString?qNameString?attributesAttributes) { 
        for (i in 0 until attributes.length) { 
            println(attributes.getQName(i) + "=" + 
                    attributes.getValue(i)) 
        } 
    } 

    @Throws(SAXException::class) 
    override fun characters(chCharArray?startIntlengthInt) { 
        //Unchanged from your implementation 
    } 

    @Throws(SAXException::class) 
    override fun endElement(uriString?localNameString?qNameString?) { 
        //  Elide code not needing change 
    } 


}

运行效果:

LastEditDate=2013-10-11T00:49:16.817 LastActivityDate=2013-10-11T00:49:16.817 CommentCount=1 Id=3013 PostTypeId=2 ParentId=2709 CreationDate=2008-08-06T01:58:18.353 Score=1 Body=

You seem to have answered your own question there Nathan ;-)

"GetLastInputInfo" is the way to go.

One trick is that if your application is running on the desktop, and the user connects to a virtual machine, then GetLastInputInfo will report no activity (since there is no activity on the host machine).

This can be different to the behaviour you want, depending on how you wish to apply the user input.

OwnerUserId=49 LastActivityDate=2008-08-06T01:58:18.353 CommentCount=1 Id=3017 PostTypeId=1 AcceptedAnswerId=3029 CreationDate=2008-08-06T02:15:14.123 Score=219 ViewCount=416897 Body=

By "generate", I mean auto-generation of the code necessary for a particular selected (set of) variable(s).

But any more explicit explication or comment on good practice is welcome.

OwnerUserId=391 LastEditorUserId=63550 LastEditorDisplayName=EnderMB LastEditDate=2019-12-09T17:56:00.820 LastActivityDate=2019-12-25T23:38:43.047 Title=How can we generate getters and setters in Visual Studio? Tags=<c#><visual-studio><setter><getter> AnswerCount=16 CommentCount=0 FavoriteCount=47 Id=3019 PostTypeId=2 ParentId=3017 CreationDate=2008-08-06T02:24:29.130 Score=0 Body=

I don't have Visual Studio installed on my machine anymore (and I'm using Linux), but I do remember that there was an wizard hidden somewhere inside one of the menus that gave access to a class builder.

With this wizard, you could define all your classes' details, including methods and attributes. If I remember well, there was an option through which you could ask Visual Studio to create the setters and getters automatically for you.

I know it's quite vague, but check it out and you might find it.

OwnerUserId=431 LastEditorUserId=63550 LastEditDate=2019-12-09T18:05:15.310 LastActivityDate=2019-12-09T18:05:15.310 CommentCount=0 Id=3020 PostTypeId=2 ParentId=1683 CreationDate=2008-08-06T02:26:35.333

Score=11

LastEditDate=2013-10-11T00:49:16.817
LastActivityDate=2013-10-11T00:49:16.817
CommentCount=1
Id=3013
PostTypeId=2
ParentId=2709
CreationDate=2008-08-06T01:58:18.353
Score=1
Body=

You seem to have answered your own question there Nathan ;-)
"GetLastInputInfo" is the way to go.

One trick is that if your application is running on the desktop, and the user connects to a virtual machine, then GetLastInputInfo will report no activity (since there is no activity on the host machine).

This can be different to the behaviour you want, depending on how you wish to apply the user input.

OwnerUserId=49
LastActivityDate=2008-08-06T01:58:18.353
CommentCount=1
Id=3017
PostTypeId=1
AcceptedAnswerId=3029
CreationDate=2008-08-06T02:15:14.123
Score=219
ViewCount=416897
Body=

By "generate", I mean auto-generation of the code necessary for a particular selected (set of) variable(s).

But any more explicit explication or comment on good practice is welcome.

OwnerUserId=391
LastEditorUserId=63550
LastEditorDisplayName=EnderMB
LastEditDate=2019-12-09T17:56:00.820
LastActivityDate=2019-12-25T23:38:43.047
Title=How can we generate getters and setters in Visual Studio?
Tags=<c#><visual-studio><setter><getter>
AnswerCount=16
CommentCount=0
FavoriteCount=47
Id=3019
PostTypeId=2
ParentId=3017
CreationDate=2008-08-06T02:24:29.130
Score=0
Body=

I don't have Visual Studio installed on my machine anymore (and I'm using Linux), but I do remember that there was an wizard hidden somewhere inside one of the menus that gave access to a class builder.

With this wizard, you could define all your classes' details, including methods and attributes. If I remember well, there was an option through which you could ask Visual Studio to create the setters and getters automatically for you.

I know it's quite vague, but check it out and you might find it.

OwnerUserId=431
LastEditorUserId=63550
LastEditDate=2019-12-09T18:05:15.310
LastActivityDate=2019-12-09T18:05:15.310
CommentCount=0
Id=3020
PostTypeId=2
ParentId=1683
CreationDate=2008-08-06T02:26:35.333
Score=11


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。