Java XMLEventReader Stax API无法获取JDK 1.8中属性的名称和值
我正在使用StatxXMLEventReader来读取XML。我必须验证xml中的几个标记,我正在使用相同的标记。我能够从xml中成功读取标记名和字符,但无法读取属性名称和值。我正在使用JDK1.8.111 XML:Java XMLEventReader Stax API无法获取JDK 1.8中属性的名称和值,java,xml,stax,Java,Xml,Stax,我正在使用StatxXMLEventReader来读取XML。我必须验证xml中的几个标记,我正在使用相同的标记。我能够从xml中成功读取标记名和字符,但无法读取属性名称和值。我正在使用JDK1.8.111 XML: <xml> <status request_id="fa844c52-daeb-4d24-920b-581ce2ac1afe1482232642212" response_time="00:00:00:039"> public static Str
<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();
}
代码:
<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();
}
public静态字符串XMLParseAttribute()抛出XMLStreamException,IOException{
XMLInputFactory inputFactory=XMLInputFactory.newInstance();
in=IOUtils.toInputStream(URLResponse,“UTF-8”);
eventReader=inputFactory.createXMLEventReader(在中);
XMLEvent=eventReader.nextEvent();
while(eventReader.hasNext())
{
XMLEvent=eventReader.nextEvent();
if(event.isStartElement()){
迭代器itr=event.asStartElement().getAttributes();
while(itr.hasNext()){
Attribute=itr.next();
属性//在此处获取名称和值
}
}
}
//下面是这样的
返回属性.getName().toString();
}
请指导我如何使用此XMLEventReader读取属性名称和值。这是一个简单的bro,简短而快速的答案是
要获取属性名称,请使用此
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);
}
}
}
}
public static void XMLParseAttribute()抛出XMLStreamException,IOException
{
XMLInputFactory inputFactory=XMLInputFactory.newInstance();
InputStream in=新文件InputStream(“input.xml”);
XMLEventReader eventReader=inputFactory.createXMLEventReader(在中);
XMLEvent事件;
while(eventReader.hasNext())
{
event=eventReader.nextEvent();
if(event.isStartElement())
{
字符串elemntName=event.asStartElement().getName().getLocalPart();
System.out.println(elemntName);
迭代器这是一个简单的兄弟,简短而快速的答案是
要获取属性名称,请使用此
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);
}
}
}
}
public static void XMLParseAttribute()抛出XMLStreamException,IOException
{
XMLInputFactory inputFactory=XMLInputFactory.newInstance();
InputStream in=新文件InputStream(“input.xml”);
XMLEventReader eventReader=inputFactory.createXMLEventReader(在中);
XMLEvent事件;
while(eventReader.hasNext())
{
event=eventReader.nextEvent();
if(event.isStartElement())
{
字符串elemntName=event.asStartElement().getName().getLocalPart();
System.out.println(elemntName);
迭代器我得到了这个,我以前导入了属性的类形式java.swing。现在我改为导入javax.xml.stream.events.Attribute。它现在可以正常工作了。错误在于导入时,我没有得到名称和值方法。好消息兄弟,请始终注意这个简单的事情,希望你永远成功(:我得到了这个,我以前导入了属性的类形式java.swing。现在我改为导入javax.xml.stream.events.Attribute。它现在可以正常工作了。错误是在导入过程中,我没有得到名称和值方法。好消息兄弟,请始终注意这个简单的事情,希望你永远成功(: