java - DOM parser, why do I get just one child of an element? -


my question "dom parser, why 1 child of element?"

i looked this , this one, not point.

what i'm trying following:

i have xml file (see extract below) :

<poitem>    <item>       <po_item>00010</po_item>       <short_text>item_a</short_text>       <matl_group>20010102</matl_group>       <agreement>4600010076</agreement>       <agmt_item>00010</agmt_item>       <hl_item>00000</hl_item>       <net_price>1.000000000</net_price>       <quantity>1.000</quantity>       <po_unit>ea</po_unit>    </item>    <item>       <po_item>00020</po_item>       <short_text>item_b</short_text>       <matl_group>20010102</matl_group>       <agreement>4600010080</agreement>       <agmt_item>00020</agmt_item>       <hl_item>00000</hl_item       <net_price>5.000000000</net_price>       <quantity>5.000</quantity>       <po_unit>ea</po_unit>    </item> </poitem> 

i want extract <po_item>, <short_text>, <matl_group>, <net_price>, <quantity> , <po_unit> , write another, smaller xml file.

so code:

nodes = dcmt.getelementsbytagname("poitem");  element rootelement2 = doc1.createelement("po_positions"); rootelement1.appendchild(rootelement2);  element details2 = doc1.createelement("po_details"); rootelement2.appendchild(details2);  (int = 0; < nodes.getlength(); i++) {     node node = nodes.item(i);     if (node.getnodetype() == node.element_node) {         element element = (element) node;          element position = doc1.createelement("position");         details2.appendchild(position);          element poitm = doc1.createelement("po_item");         poitm.appendchild(doc1.createtextnode(getvalue("po_item", element)));         position.appendchild(poitm);          element matlgrp = doc1.createelement("matl_group");         matlgrp.appendchild(doc1.createtextnode(getvalue("matl_group",element)));         position.appendchild(matlgrp);          element pounit = doc1.createelement("po_unit");         pounit.appendchild(doc1.createtextnode(getvalue("po_unit",element)));         position.appendchild(pounit);          element netprice = doc1.createelement("net_price");         netprice.appendchild(doc1.createtextnode(getvalue("net_price",element)));         position.appendchild(netprice);          element shorttxt = doc1.createelement("short_text");         shorttxt.appendchild(doc1.createtextnode(getvalue("short_text",element)));         position.appendchild(shorttxt);          //element matl = doc2.createelement("material");         //matl.appendchild(doc2.createtextnode(getvalue("material",element)));         //details2.appendchild(matl);          element qnty = doc1.createelement("quantity");         qnty.appendchild(doc1.createtextnode(getvalue("quantity",element)));         position.appendchild(qnty);          /*element preqnr = doc1.createelement("preq_no");           preqnr.appendchild(doc1.createtextnode(getvalue("preq_no",element)));           details2.appendchild(preqnr); */     } } 

so far good, i'm getting new xml file, holds first entry, understand it, nodes = dcmt.getelementsbytagname("poitem"); gets first <item> until first </item> , gets out of loop. how manage step next item? need create kind of loop, access next <item> ? way, changing structure of xml file no option, since file interface. or make mistake while writing new xml file?

the output looks this:

<po_positions>   <po_details>     <position>       <po_item>00010</po_item>       <matl_group>20010102</matl_group>       <po_unit>ea</po_unit>       <net_price>1.00000000</net_price>       <short_text>item_a</short_text>       <quantity>1.000</quantity>     </position>   </po_details> </po_positions> 

you parse yourself, it's kind of pain. when did xml way when, used use stylesheets these kinds of transformations. post: how transform xml xsl using java

if that's not option, hand (i omitted new document construction, can see goes):

import java.io.file; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.junit.test; import org.w3c.dom.document; import org.w3c.dom.node; import org.w3c.dom.nodelist;  public class xmltest {      @test     public void testxmlparsing() throws exception {         documentbuilderfactory dbfactory = documentbuilderfactory.newinstance();         documentbuilder dbuilder = dbfactory.newdocumentbuilder();         document doc = dbuilder.parse(new file("/users/aakture/documents/workspace-sts-2.9.1.release/smartfox/branches/trunk/java/gelato/src/test/resources/sample.xml").getabsolutepath());         node poitem = doc.getelementsbytagname("poitem").item(0);         nodelist poitemchildren = poitem.getchildnodes();         (int = 0; < poitemchildren.getlength(); i++) {             node item = poitemchildren.item(i);             nodelist itemchildren = item.getchildnodes();             (int j = 0; j < itemchildren.getlength(); j++) {                 node itemchild = itemchildren.item(j);                 if("po_item".equals(itemchild.getnodename())) {                     system.out.println("found po_item: " +     itemchild.gettextcontent());                 }                 if("matl_group".equals(itemchild.getnodename())) {                     system.out.println("found matl_group: " + itemchild.gettextcontent());                 }              }         }     } } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -