0

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( "Start Element 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 }Date。

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 claSS

ありがとう!

4

2 に答える 2

1

答えは簡単です。BookCatalogのxmlで名前空間を定義しました。

<BookCatalog xmlns = "http://www.publishing.org">

異なる名前空間に同じ名前のタグがある場合、これが違いを生む方法です!

お役に立てれば!

于 2013-03-01T18:52:48.633 に答える
1

1)試してみてください

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

2)XMLStreamReaderは同じ仕事をすることができますが、より高速です

于 2013-03-02T04:33:55.003 に答える