gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:09:03 25 4
gpt4 key购买 nike

我正在使用 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">

代码:

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 来读取属性名称和值。

最佳答案

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

要获取属性名称,请使用此

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

要获取属性值,请使用此

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

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

关于java - XMLEventReader Stax API 无法获取 JDK 1.8 中属性的名称和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41338521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com