0

SAXParser は初めてなので、ご容赦ください。

XML ファイルを解析して .xml に変換するにはどうすればよいList<XNode>ですか? 以下は、クラス XNode の構造です。

class XNode{

    private String nodeName;
    private String nodeValue;
    private List<XAttribute> attributes;
    private boolean isParentNode;
    private List<XNode> childNodes;
}

また、XAttribute の構造:

class XAttribute{

    private String name;
    private String value;
}

ファイルを解析すると、List オブジェクトが返されます。

これまでのところ、以下のコードを試しましたが、childNodes を確認してアタッチする方法がわかりません。

public class XmlProcesser extends DefaultHandler {
    XMLResponse xmlResponse = null;
    boolean endtag = false;

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        System.out.print("" + qName + "");
        if (attributes.getLength() == 0) {
        } else {
            for (int index = 0; index < attributes.getLength(); index++) {
                System.out.print(attributes.getLocalName(index) + " =  " + attributes.getValue(index));
            }
        }
    }

    @Override
    public void characters(char ch[], int start, int length) throws SAXException {
        String s = new String(ch, start, length);
        System.out.println(s);
        endtag = false;
    }

    @Override
    public void endElement(String uri, String localName,
            String qName) throws SAXException {
        endtag = true;
        System.out.print("  " + qName + "  ");

    }
}
4

2 に答える 2

0

これを行う通常の方法の 1 つはStack、現在のノードの概念を使用することです。に遭遇しstartElementたら、次のことを行います

  1. 新しい要素を作成するchild
  2. childこの要素を要素にcurrent追加します
  3. current要素をスタックにプッシュする
  4. 要素childを新しいcurrent要素にします。

あなたが遭遇したとき、あなたendElementは逆を行います:

  1. から一番上の要素をポップしstackて、current要素を再度作成します。

スタックの一番下はroot.

于 2012-10-09T23:37:24.697 に答える
0
Pop the top element from the stack and make the current element again.

Pop the top element from the stack and make the current element again.

Pop the top element from the stack and make the current element again.

Pop the top element from the stack and make the current element again.
于 2013-02-01T12:21:07.003 に答える