简体   繁体   English

XMLEventReader Stax API无法在JDK 1.8中获取属性的名称和值

[英]XMLEventReader Stax API not able to getName and value for Attribute in JDK 1.8

I am using Stax XML EventReader for reading from xml. 我正在使用Stax XML EventReader从xml读取。 I have to verify a few tags in xml for which i am using the same. 我必须在xml中验证一些我正在使用的标签。 I am able to successfully read the tagname and characters from the xml but unable to read the attribute name and value. 我能够从xml成功读取标记名和字符,但无法读取属性名称和值。 I am using jdk 1.8.111 我正在使用jdk 1.8.111

XML: XML:

<xml>
<status request_id="fa844c52-daeb-4d24-920b-581ce2ac1afe1482232642212"     response_time="00:00:00:039">

CODE: 码:

public static String XMLParseAttribute() throws XMLStreamException, IOException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    in = IOUtils.toInputStream(URLResponse, "UTF-8");
    eventReader = inputFactory.createXMLEventReader(in);                
    XMLEvent event = eventReader.nextEvent();

        while(eventReader.hasNext())
        {
            XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {
                    Iterator<Attribute> itr = event.asStartElement().getAttributes();
                    while(itr.hasNext()){
                        Attribute attribute = itr.next();
                        attribute. //get name and value here
                    }
                }
             }
        //Something like this below
        return attribute.getName().toString();
        }

Kindly guide me as to how to use this XMLEventReader to read the attribute name and value. 请指导我如何使用此XMLEventReader读取属性名称和值。

It's an easy bro, the short and quick answer is 这是一个简单的兄弟,简短而快速的答案是

to get the attribute name use this 获取属性名称使用此

String name = attribute.getName().toString();

to get the attribute value use this 获取属性值使用此

String value = attribute.getValue();

the full code for your method (I eliminated the return type) and re-arranged the code 您方法的完整代码(消除了返回类型),然后重新排列了代码

public static void XMLParseAttribute() throws XMLStreamException, IOException
{

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    InputStream in = new FileInputStream("input.xml");
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    XMLEvent event;

    while (eventReader.hasNext())
    {
        event = eventReader.nextEvent();

        if (event.isStartElement())
        {
            String elemntName = event.asStartElement().getName().getLocalPart();
            System.out.println(elemntName);
            Iterator<Attribute> iterator = event.asStartElement().getAttributes();
            while (iterator.hasNext())
            {
                Attribute attribute = iterator.next();
                String value = attribute.getValue();
                String name = attribute.getName().toString();
                System.out.println("\t" + name + " " + value);
            }
        }
    }
}

and here's a complete code 这是完整的代码

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.xml.stream.*;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.XMLEvent;

public class XmlReader
{

public static void main(String[] args) throws XMLStreamException, IOException
{
    XMLParseAttribute("input.xml");
}

public static void XMLParseAttribute(String fileName) throws XMLStreamException, IOException
{

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    InputStream in = new FileInputStream(fileName);
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    XMLEvent event;

    while (eventReader.hasNext())
    {
        event = eventReader.nextEvent();

        if (event.isStartElement())
        {
            String elemntName = event.asStartElement().getName().getLocalPart();
            System.out.println(elemntName);
            Iterator<Attribute> iterator = event.asStartElement().getAttributes();
            while (iterator.hasNext())
            {
                Attribute attribute = iterator.next();
                String value = attribute.getValue();
                String name = attribute.getName().toString();
                System.out.println("\t" + name + " " + value);
            }
        }
    }
}}

hope this is useful and solve your problem (: 希望这对您有用并解决您的问题(:
you can also have a look for this simple tutorial java xml on jenkov 您也可以在jenkov上查看这个简单的教程java xml

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

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