java 7 new feature

  1. diamond operator.
    泛型只需要在泛型声明时声明具体类型,如:
    List list=new ArrayList<>();
    注意 必须带<>符号。

  2. using primitive,enum or string in switch statements.
    switch语句支持string,比较时使用equal

  3. automatic resource management
    notice that the resource should be managed automaticly must implement java.lang.AutoCloseable interface.
    the AutoCloseable is the parent of java.io.Closeable interface and has just one method close() that would be called by jvm when the control comes out of the try block.
    try语句自动支持对资源对象的回收管理,这些资源对象必须实现AutoCloseable接口,如 java.io.Closeable
    the code template like this :
    try(
    InputStream readIn=new FileInputStream(path);
    OutputStream writeOut=new FileOutputStream(otherPath)
    ){
    // doSmothing
    }

  4. numeric literals with underscores
    notice that the binary literals are also release too.
    数字字面量可以使用下划线隔开,二进制字面量也直接支持,如:
    1_000_000
    0b1001

  5. improved exception handling.
    在catch语句参数中可以捕获多个异常类型,每个异常类型通过符号 | 隔开

  6. new file system api(nio 2.0).
    (1) working with path:a new java.nio.file package consists of classes and interfaces such as path,paths,files,FileSystems and others.
    (2) file change notifications:
    a. create a WatchService,the service consists of a queue to hold watchkeys.
    b. register the director/file you wish to monitor with this WatchService.
    c. while registering,specify the types of events you wish to receive(create,modify,delete events).
    d. when an events occurs,a watchkey is placed into the queue.

  7. fork and join
    ForkJoinPool,RecursiveAction,RecursiveTask.

<think>嗯,用户想找关于Javafeature.getAttribute方法的用法或解释。首先,我需要确定“feature.getAttribute”具体指的是哪个类或上下文。因为在Java标准库中并没有一个广泛使用的名为“feature”的类带有getAttribute方法,所以这可能涉及到特定的框架或库。 根据用户提供的引用内容,引用[1]提到了Spring-WS中的@XPathParam参数和命名空间注解,引用[2]则涉及XML解析中的XMLStreamReader。这可能暗示用户的问题与XML处理相关,或者是在某个框架(如Spring Web Services)中使用getAttribute方法。 首先,我应该考虑常见的Java API中有没有类似的方法。例如,在XML处理中,如Document对象或Element接口,通常有getAttribute方法。比如,org.w3c.dom.Element中的getAttribute(String name)方法用于获取属性的值。此外,Java的XMLStreamReader类也有getAttribute方法,用于读取XML元素的属性。 在引用[2]中,用户提到了XMLStreamReader的使用,这可能是一个线索。XMLStreamReader的getAttribute方法可以按索引或名称获取属性值。例如,在解析XML时,遍历属性并获取它们的值。 另外,用户可能是在使用某个框架,比如Spring-WS中的特性配置,但根据引用[1],里面没有直接提到getAttribute方法,而是关于参数绑定和命名空间的处理。因此,可能用户的问题更偏向于XML处理部分。 接下来,我需要确认用户的问题是否需要示例代码。用户询问的是用法或示例,所以最好提供一个具体的例子,比如使用DOM解析或StAX解析时如何使用getAttribute方法。 另外,需要注意用户提到的“feature.getAttribute”中的“feature”是否指某个特定类的实例。例如,在XMLStreamReader中,可能通过工厂设置特性(feature),但这里的“feature”可能是指某个配置属性,如factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);中的属性设置,但这属于setProperty方法,而不是getAttribute。 可能的混淆点在于,“feature”可能被误解为XML元素中的属性,而实际上在XML中,属性是attribute,而feature可能指解析器的特性。需要区分这两个概念。 因此,正确的方向是解释在XML处理中,如何从元素中获取属性值,使用Element.getAttribute()或XMLStreamReader.getAttributeValue()等方法,并提供相应的代码示例。同时,如果用户的问题涉及到特定框架中的方法,可能需要更多的上下文,但根据现有的引用,XML处理的可能性更大。 总结,回答应包括DOM和StAX两种常见的XML解析方式中使用getAttribute方法的示例,并指出可能的常见错误,如属性不存在时的返回值。此外,需要确认用户是否指的是其他上下文,如特定库中的方法,但基于现有信息,以XML处理为例进行说明最为稳妥。</think>在Java中,`getAttribute()`方法常见于XML解析场景,根据上下文可分为两种典型用法: ### 一、DOM解析器中的Element.getAttribute() 在`org.w3c.dom.Element`接口中定义的标准方法,用于获取XML元素的属性值: ```java DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("data.xml")); Element element = (Element) doc.getElementsByTagName("book").item(0); String isbn = element.getAttribute("isbn"); // 获取<book isbn="xxx">中的属性值 ``` * 若属性不存在返回空字符串 * 需处理`ClassCastException`和`DOMException` ### 二、StAX解析器中的XMLStreamReader.getAttributeValue() 在流式XML解析时通过索引或命名空间获取属性: ```java XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream("data.xml")); while (reader.hasNext()) { if (reader.getEventType() == XMLStreamReader.START_ELEMENT) { String attrValue = reader.getAttributeValue(null, "isbn"); // 无命名空间 System.out.println("ISBN: " + attrValue); } reader.next(); } ``` * 索引访问:`reader.getAttributeValue(int index)` * 命名空间访问:`reader.getAttributeValue(String nsURI, String localName)` ### 三、常见错误场景 1. **空指针异常**:未检查元素是否存在直接调用 ```java // 错误示例 Element nullElement = null; String value = nullElement.getAttribute("name"); // 抛出NullPointerException ``` 2. **命名空间未声明**:未正确指定命名空间前缀[^1] ```java // 需要配合@Namespace注解声明命名空间 @Namespaces(@Namespace(prefix = "ns", uri="http://example.com")) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值