XML 파일 내에서 시작 태그 요소 이름을 추출하는 방법을 알 수 없습니다. 나는 가까이에 ~ 오류 없음을 의미하고 태그 이름을 얻고 있지만 태그 이름과 정보를 얻고 있습니다. 내가 얻고 것은 :JAVA StAX 추출
{http://www.publishing.org}author
{http://www.publishing.org}Date
{http://www.publishing.org}ISBN
내 최종 결과는, 내 프로그램이 나에게주고 싶어 : 나는이
<?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>
:
이Author
Data
ISBN
첫째, 여기 내 XML 파일입니다 시도했습니다 System.out.println ("시작 요소 GetName :"+ se.getName()); System.out.println ("Start Element 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 수율 : 통화.
날짜, 작성자 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
} // 공용 클래스
감사 :
여기 내 코드입니다!
어떻게 네임 스페이스가있는 XML로 등 단지 날짜, ISBN을 얻을 수 있습니까? 감사! – user638361