2

XML ドキュメントのコンテンツへのアクセスに問題があります。私の目標は次のとおりです。XML ソースを取得し、連想配列と同等のものに解析してから、永続化可能なオブジェクトとして保存します。

xml は非常に単純です。

<root>
<element>
    <category_id>1</category_id>
    <name>Cars</name>
</element>
<element>
    <category_id>2</category_id>
    <name>Boats</name>
</element>
</root>

以下の基本的な Java クラス。上記の http 応答の後に save(xml) を呼び出すだけです。はい、xml は適切にフォーマットされています。

    import java.io.IOException;
    import java.util.Hashtable;

    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    import java.util.Vector;
    import net.rim.device.api.system.PersistentObject;
    import net.rim.device.api.system.PersistentStore;
    import net.rim.device.api.xml.parsers.DocumentBuilder;
    import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

public class database{
        private static PersistentObject storeVenue;
        static final long key = 0x2ba5f8081f7ef332L;
        public Hashtable hashtable;
        public Vector venue_list;
        String _node,_element;

    public database()
    {
        storeVenue = PersistentStore.getPersistentObject(key);
    }

    public void save(Document xml)
    {
            venue_list = new Vector();
        storeVenue.setContents(venue_list);


        Hashtable categories = new Hashtable();


        try{

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            docBuilder.isValidating();

            xml.getDocumentElement ().normalize ();

            NodeList list=xml.getElementsByTagName("*");
            _node=new String();
            _element = new String();

            for (int i=0;i<list.getLength();i++){

                Node value=list.item(i).getChildNodes().item(0);
                _node=list.item(i).getNodeName();
                _element=value.getNodeValue();

                categories.put(_element, _node);
            }

        }
        catch (Exception e){
            System.out.println(e.toString());
        }

        venue_list.addElement(categories);


        storeVenue.commit();
    }

上記のコードは進行中の作業であり、おそらく重大な欠陥があります。しかし、私は何日もこれに取り組んでいます。すべての子ノード、または名前と値のペアを取得できないようです。ベクトルを文字列として出力すると、通常は [{ = root, = element}] のような結果になります。「category_id」も「name」もありません

理想的には、[{1 = 車、2 = ボート}] のような結果になるでしょう。

どんな助けでも大歓迎です。

ありがとう

4

3 に答える 3

1

Here's a fixed version of your program. Changes that I made are as follows:

  • I removed the DocBuilder-stuff from the save() method. These calls are needed to construct a new Document. Once you have such an object (and you do since it is passed in as an argument) you don't need the DocumentBuilder anymore. A proper use of DocumentBuilder is illustrated in the main method, below.
  • _node,_element need not be fields. They get new values with each pass through the loop inside save so I made them local variables. In addition I changed their names to category and name to reflect their association with the elements in the XML document.
  • There's never a need to create a new String object by using new String(). A simple "" in enough (see the initialization of the category and name variables).
  • Instead of looping over everything (via "*") the loop now iterates over element elements. Then there is a an inner loop that iterates over the children of each element, namely: its category_id and name elements.
  • In each pass of the inner we set either the category or the name variable depending on the name of the node at hand.
  • The actual value that is set to these variables is obtained by via node.getTextContent() which returns the stuff between the node's enclosing tags.

class database:

  public class database {
     private static PersistentObject storeVenue;
     static final long key = 0x2ba5f8081f7ef332L;
     public Hashtable hashtable;
     public Vector venue_list;

     public database() {
        storeVenue = PersistentStore.getPersistentObject(key);
     }

     public void save(Document xml) {
        venue_list = new Vector();
        storeVenue.setContents(venue_list);

        Hashtable categories = new Hashtable();

        try {

           xml.getDocumentElement().normalize();

           NodeList list = xml.getElementsByTagName("element");

           for (int i = 0; i < list.getLength(); i++) {

              String category = "";
              String name = "";
              NodeList children = list.item(i).getChildNodes();
              for(int j = 0; j < children.getLength(); ++j)
              {
                 Node n = children.item(j);
                 if("category_id".equals(n.getNodeName()))
                    category = n.getTextContent();
                 else if("name".equals(n.getNodeName()))
                    name = n.getTextContent();
              }

              categories.put(category, name);

              System.out.println("category=" + category + "; name=" + name);
           }

        } catch (Exception e) {
           System.out.println(e.toString());
        }

        venue_list.addElement(categories);

        storeVenue.commit();
     }
  }

Here's a main method:

  public static void main(String[] args) throws Exception {
     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
        .newInstance();
     DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
     docBuilder.isValidating();

     Document xml = docBuilder.parse(new File("input.xml"));

     database db = new database();
     db.save(xml);
  }
于 2010-04-10T07:54:04.570 に答える
0
 //res/xml/input.xml
 private static String _xmlFileName = "/xml/input.xml";

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = factory.newDocumentBuilder();
 InputStream inputStream = getClass().getResourceAsStream( _xmlFileName );
 Document document = builder.parse( inputStream );
于 2010-11-08T12:03:12.863 に答える
0

どうもありがとう。ほんの少しの変更で、私が探していたものを正確に実行できました。

私がしなければならなかった変更は次のとおりです。category = n.getFirstChild().getNodeValue(); を使用する必要がありました。各ノードの値を取得します。ビルド設定を更新するなどの簡単な解決策があったかもしれませんが、デフォルトの推奨ビルド設定から逸脱しても安全な場合を知るには、BB の要件に精通していません。

主に、次の行を変更する必要がありました。

 Document xml = docBuilder.parse(new File("input.xml"));

私のWebサーバーから配信されたInputStreamから読み取っていたので、必ずしもローカルファイルではありませんでした.xmlをローカルに保存する方が、ハッシュテーブルでいっぱいのベクトルを保存するよりも効率的かどうか疑問に思っています. ...

InputStream responseData = connection.openInputStream();
 Document xmlParsed = docBuilder.parse(result);

明らかに、これを読みやすくするために、HTTP 接続部分をスキップしました。

あなたの助けのおかげで、ブラインド デバッグの週末をまるまる節約できました。どうもありがとうございました!この投稿が他の誰かにも役立つことを願っています。

于 2010-04-10T16:15:42.713 に答える