简体   繁体   English

如果在jsoup中定义了元素,分配给变量的最干净方法是?

[英]Cleanest way to assign to a variable if an element in jsoup is defined?

I was doing this String name = doc.select("h1#name").first().text(); 我正在执行此String name = doc.select("h1#name").first().text(); and I received a NullPointerException on a page where the 'h1#name' element did not exist. 并且在“ h1#name”元素不存在的页面上收到了NullPointerException。 I'm parsing the DOM, grabbing hundreds of elements. 我正在解析DOM,获取了数百个元素。 It seems that I need to test each element before I assign, so I changed it to this: 似乎我需要在分配每个元素之前对其进行测试,因此我将其更改为:

String name = null;

if( doc.select("h1#name").first() != null && doc.select("h1#name").first().text() != null ))
    name = doc.select("h1#name").first.text();

Is there a better way to do it? 有更好的方法吗? I'm just learning Java, and my background is in Perl, where I'd do something like this: 我只是在学习Java,而我的背景是Perl,在那儿我会做这样的事情:

my $name = $doc->select("h1#name")->text if $doc->select("h1#name");

It's not a big deal, since my code does work for me. 没什么大不了的,因为我的代码确实对我有用。 I was just wondering if there was a cleaner way to do this in Java. 我只是想知道在Java中是否有更干净的方法可以做到这一点。

You will not come arround checking all objects you go throuth to access the value you want. 您将不会检查所有要访问的对象以访问所需的值。

To translate your perl syntax into java I would use the ternary operator : 要将您的perl语法转换为java,我将使用三元运算符

name = doc.select("h1#name").first() != null ? 
            doc.select("h1#name").first().text() : null

There is no need to check doc.select("h1#name).first().text() != null too unless you have a non null value in name and you don't want to override it with null. 也无需检查doc.select("h1#name).first().text() != null除非您的名称中包含非null值并且您不想使用null覆盖它。

I would suggest a utility method, something like this: 我会建议一种实用程序方法,如下所示:

Document mDocument;

(...)

String getElementTextOrNull (String cssQuery) {
    Element e = mDocument.select(cssQuery).first();

    if (e == null) {
        return null;
    } else {
        return e.text();
    }
}

Then, you can eliminate a lot of boilerplate and repetition, so your code simply becomes: 然后,您可以消除很多样板和重复,因此您的代码将变成:

String name = getElementTextOrNull("h1#name");

You can also do other things in that method, like checking if the text is empty or if it is valid. 您还可以使用该方法执行其他操作,例如检查文本是否为空或有效。

Of course, this may be impractical if you only get the text of a specific element once, but this is what I would suggest otherwise. 当然,如果仅一次获取特定元素的文本,这可能是不切实际的,但这是我不建议的。 Personally, I also think this is neater and more concise than the ternary operator solution proposed in the other answer. 就我个人而言,我也认为这比另一个答案中提出的三元运算符解决方案更简洁,更简洁。

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

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