Meaningless Drivel is fun!*
The moose likes XML and Related Technologies and the fly likes Create node /   org.w3c.dom.Node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Create node /   org.w3c.dom.Node" Watch "Create node /   org.w3c.dom.Node" New topic
Author

Create node / org.w3c.dom.Node

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
The Doc before:

<soap:Envelope>
<soap:Header>
<ns2:technicalAddress>
</ns2:technicalAddress>
<ns2:serviceHeader>
<organisationIdentification>
<mainRegistrationNumber>20</mainRegistrationNumber>
<isoCountryCode>dk</isoCountryCode>
</organisationIdentification>
<erpInformation>
<erpsystem>Plexa</erpsystem>
<erpversion>1</erpversion>
</erpInformation>
<format>iso</format>
<createDateTime>2014-06-10T16:07:29.211+02:00</createDateTime>
</ns2:serviceHeader>
</soap:Header>
<soap:Body>
<ns2:payment></ns2:payment>
</soap:Body>
</soap:Envelope>




Wanted:
<soap:Envelope>
<soap:Header>
<ns2:technicalAddress>
<ipAddress>127.0.0.1</ipAddress>
</ns2:technicalAddress>
<ns2:serviceHeader>
<organisationIdentification>
<mainRegistrationNumber>20</mainRegistrationNumber>
<isoCountryCode>dk</isoCountryCode>
</organisationIdentification>
<erpInformation>
<erpsystem>Plexa</erpsystem>
<erpversion>1</erpversion>
</erpInformation>
<format>iso</format>
<createDateTime>2014-06-10T16:07:29.211+02:00</createDateTime>
</ns2:serviceHeader>
</soap:Header>
<soap:Body>
<ns2:payment></ns2:payment>
</soap:Body>
</soap:Envelope>


But the ipAddress is not added

What the f... is wrong here ?

Frank
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

How do you know? I can't see any code that displays the result. Please post an SSCCE.
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
I know, i have a print function that works... to keep it simple i havent added this method...

But i can se i get a nullpointer on this line:

nodes.item(0).appendChild(p);

But i still dont get an idea, how to add the node to <ns2:technicalAddress>

Frank
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

So either nodes or item(0) is null. I would fix that first. Check the API for when null may be returned from methods.
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
.item(0) is null, but cant find out why null, the technicalAddress is in the XML as you see.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
As I recall, you don't want to work with the Document node, instead use the Document method getDocumentElement() to get an Element that will handle the getElementsByTagName correctly.

Bill
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
I tried this out, but still i get a null in the nodes.item(0)... Its not easy .. Hmmm




Frank
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

Your code is looking for a "technicalAddress" element. But there aren't any such elements in your document. You have elements whose name is "ns2:technicalAddress" so you should be using the getElementsByTagNameNS method. Make sure to use the namespace URL which belongs to the "ns2" prefix.

(You did make your parser namespace-aware, didn't you?)
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
Thanks a lot... It works ...


Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
It works... BUT...

This is my namespace in the XML.


This works:


But is there a way to get the targetnamespace in java ?


Frank
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create node / org.w3c.dom.Node