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

时间:2016-12-27 03:06:38

标签: java xml stax

我使用Stax XML EventReader从xml读取。我必须在xml中验证我使用相同的几个标签。我能够成功读取xml中的标记名和字符,但无法读取属性名称和值。我正在使用jdk 1.8.111

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();
        }

请指导我如何使用此XMLEventReader读取属性名称和值。

1 个答案:

答案 0 :(得分:1)

这是一个简单的兄弟,简短而快速的答案是

获取属性名称使用此

String value = attribute.getValue();

获取属性值使用此

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

您方法的完整代码(我删除了返回类型) 并重新安排了代码

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

这是一个完整的代码

<script type="text/javascript" src="~/Script/Modal.js"></script>

希望这很有用并解决你的问题(: 您还可以查看这个简单的教程java xml on jenkov