gpt4 book ai didi

JAVA StAX 提取

转载 作者:行者123 更新时间:2023-12-02 07:14:07 39 4
gpt4 key购买 nike

我不知道如何提取 XML 文件中的开始标记元素名称。我很接近〜意味着没有错误,我正在获取标签名称,但我正在获取标签名称加上信息。我得到的是:

{http://www.publishing.org}author
{http://www.publishing.org}Date
{http://www.publishing.org}ISBN

我的最终结果,我希望我的程序给我:

Author
Data
ISBN

首先,这是我的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<BookCatalog xmlns = "http://www.publishing.org">
<Book>
<Title>Yogasana</Title>
<author>Dhirenda</author>
<Date>1966</Date>
<ISBN>81-40</ISBN>
<Publisher>Dhirenda</Publisher>
<Cost Currency = "INR">11.50</Cost>
</Book>
<Book>
<Title>Yogasana</Title>
<author>J. K</author>
<Date>1954</Date>
<ISBN>0-06</ISBN>
<Publisher>Harper</Publisher>
<Cost Currency = "INR">2.95</Cost>
</Book>
</BookCatalog>

我已经尝试过了 System.out.println("起始元素GetName:"+ se.getName()); System.out.println("起始元素GetEventType: "+ se.getEventType()); System.out.println("属性名称:"+ attribute.getValue()); System.out.println("属性名称:"+ attribute.getName());

getname 产生:{ http://www.publishing.org }日期。

getEventType 产生:1

attribute.getValue 产生:INR

attribute.getName 产生:Currency。

如何获得仅包含元素名称的结果:日期、作者 ISBN 等?

为什么是 getName,我相信这是附加文本的正确方法:{ http://www.publishing.org }?

这是我的代码:

public static void main(String[] args) throws FileNotFoundException, XMLStreamException
{
// TODO code application logic here
//System.out.println("Here");
//String filename = null;
String filename = "BookCatalog.xml";
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader = factory.createXMLEventReader(new FileReader(filename));

String dataread = null;

while(reader.hasNext())
{
XMLEvent event = reader.nextEvent();
XMLEvent nextEvent = reader.peek();
switch (event.getEventType())
{
case XMLEvent.START_ELEMENT:
StartElement se = event.asStartElement();

//StartElement se = (StartElement) event;
System.out.println("Start Element GetName: " + se.getName());
System.out.println("Start Element GetEventType: " + se.getEventType());

Iterator iterator = se.getAttributes();
while (iterator.hasNext()) {
Attribute attribute = (Attribute) iterator.next();
QName name = attribute.getName();
//String value = attribute.getValue();
//System.out.println("Attribute name/value: " + name + "/" + value);
//System.out.println("Attribute name: " + name);
System.out.println("Attribute Value: " + attribute.getValue());
System.out.println("Attribute name: " + attribute.getName());
}



//System.out.println("Here");
//System.out.print("<" + se.getName());

//System.out.print(" " + se.getName());
//Iterator iterator = se.getAttributes();
//System.out.print(" " + se.getAttributes());

System.out.printf("\n");
String elem = se.getName().toString();
//String elem = se.getAttributes().toString();
//String ele = event.getAttributeName();
//if( se.getName().toString() == "{http://www.publishing.org}Date")
if( se.getName().toString().equals("{http://www.publishing.org}Date"))
//if( elem == "1")
{
dataread = reader.getElementText();
//System.out.printf("data = %s\n",reader.getElementText());
System.out.printf("data = %s\n",dataread);

}
Iterator attributes = se.getNamespaces();

while(attributes.hasNext())
{
Attribute attr= (Attribute)attributes.next();
//System.out.print(" " + attr.getName() + "=\"" +attr.getValue() +"\"");
//System.out.printf("\n");
}//end while loop
//System.out.print(">");
if(nextEvent.isCharacters())
{
Characters c = reader.nextEvent().asCharacters();
if(!c.isWhiteSpace())
//System.out.print(c.getData());
System.out.printf("\n");
}// end if
/*case XMLEvent.END_ELEMENT>
EndElement ee = event.asEndElement();
System.out.print("</"+ee.getName()+">");
break;
* */
}// end witch
}// end while
System.out.printf("FINAL data = %s\n",dataread);

reader.close();
}//end Main

}//公共(public)类

谢谢!

最佳答案

1) 尝试

System.out.println("Start Element GetName: " + se.getName().getLocalPart());

2) XMLStreamReader 可以完成相同的工作,但速度更快

关于JAVA StAX 提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164584/

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