• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the full TagPath for DOM node

 
Tom Stevns
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello XML'lers

Hopefully someone can help or guide me getting the fullTagPath for a DOM node.

The intention about the code snippet is that all XML-nodevalues should be
placed in HasMap with the fullTagPathes as keys

As an example: for the node <FogP:Surname> mentioned XML below I would like to get the path:
<atps:ATPSPdocument><atps ocumentHeader><FogP ocumentSenderContactName>


<atps:ATPSPdocument>
<atps ocumentHeader>
<FogP ocumentVersion>1.0</FogP ocumentVersion>
<FogP ocumentResponsible>FogP</FogP ocumentResponsible>
<FogP ocumentReceiver>
<FogP:IdentityQualifier>yyy</FogP:IdentityQualifier>
<FogP:IdentityValue>12345</FogP:IdentityValue>
</FogP ocumentReceiver>
<FogP ocumentSenderContactName>
<FogP:Surname>F. Nielsen</FogP:Surname>
</FogP ocumentSenderContactName>
</atps ocumentHeader>
<atps:ATPSPdocument>


I have tried nearly everything concerning methods for siblings childs and parents - and my code has just become so nasty that I feel it's the right time to ask experienced people from this site

It would be a great help for me if you have a bright solution for the problem

Thanx in advance
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a feeling that whatever you've got is pretty much the only way to accomplish what you described, i.e. loop through all elements and if an element has some text content, resolve the "XPath location" for the node using Node#getParentNode() recursively, and add the resulting {"xxx/yyy/zzz", "foobar"} into the HashMap.
 
Tom Stevns
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lasse !

Thanx for Your advice

I succeeded with the method shown below

It's simple and stupid but it works

But wouldn't i be very nice to have a DOMparser method covering this
problem

method as follows:

public List genererDocumentHashMap(String xmlString) {
HashMap hashMap = new HashMap();
List arrayList = new ArrayList();
try {
InputStream is = getInputStream(xmlString);
DOMParser parser = new DOMParser();
InputSource source= new InputSource(is);
parser.parse(source);

Document doc = parser.getDocument();
NodeList list = doc.getElementsByTagName("*");
String lastNode = "";
String lastNodeString = "";
String lastNodeStringAccountOwner = "";
Node lastParentNode = null;
NodeList childList = null;
boolean intoHierarki1 = false;
boolean intoHierarki2 = false;
boolean justCut= false;
int childListLength= 0;
String previousSiblingSubsString = "";


String aTPSPdocumentTypeX = "";
String aTPSPdocumentType1 = "atps:ATPSPdocumentType1";
String aTPSPdocumentType3 = "atps:ATPSPdocumentType3";
String aTPSPdocumentType5 = "atps:ATPSPdocumentType5";
String aTPSPdocumentType7 = "atps:ATPSPdocumentType7";
String aTPSPdocumentType12 = "atps:ATPSPdocumentType12";
String aTPSPdocumentType13 = "atps:ATPSPdocumentType13";
String aTPSPdocumentType21 = "atps:ATPSPdocumentType21";
String aTPSPdocumentType31 = "atps:ATPSPdocumentType31";
String aTPSPdocumentType33 = "atps:ATPSPdocumentType33";
String aTPSPdocumentType34 = "atps:ATPSPdocumentType34";
String aTPSPdocumentType35 = "atps:ATPSPdocumentType35";
String aTPSPdocumentType41 = "atps:ATPSPdocumentType41";
String aTPSPdocumentType42 = "atps:ATPSPdocumentType42";




for (int i=0; i<list.getLength(); i++) {
// Get element
Element element = (Element)list.item(i);
System.out.println("element " + i + " " + element.getNodeName() + " " + element.getFirstChild().getNodeValue());
if ((element.getNodeName().equals("atps:mDocumentBody"))) {
System.out.print("atpDocumentBodyHit " + atpDocumentBodyHit);
System.out.println("hashMap in dok" + hashMap.toString());
arrayList.add(atpDocumentBodyHit, hashMap);
hashMap = new HashMap();
atpDocumentBodyHit++;

}


//hop i XML hierarkiet
if ("atps:ATPSPsubmission".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("atps:ATPSPsubmissionHeader".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPsubmissionHeader//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("FogP:SubmissionSender".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPsubmissionHeader//FogP:SubmissionSender//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("FogP:SubmissionReceiver".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPsubmissionHeader//FogP:SubmissionReceiver//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("atps:ATPSPdocument".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}

if ("atps:mDocumentHeader".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentHeader//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("FogP:mDocumentSender".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentHeader//FogP:mDocumentSender//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("FogP:mDocumentReceiver".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentHeader//FogP:mDocumentReceiver//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("FogP:mDocumentSenderContactName".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentHeader//FogP:mDocumentSenderContactName//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("FogP:mDocumentSenderContactName".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentHeader//FogP:mDocumentSenderContactName//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("atps:mDocumentBody".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
//S�t variablen for DocumentType
//1
if (aTPSPdocumentType1.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType1;
}
//3
if (aTPSPdocumentType3.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType3;
}
//5
if (aTPSPdocumentType5.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType5;
}
//7
if (aTPSPdocumentType7.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType7;
}
//12
if (aTPSPdocumentType12.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType12;
}
//13
if (aTPSPdocumentType13.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType13;
}
//21
if (aTPSPdocumentType21.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType21;
}
//31
if (aTPSPdocumentType31.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType31;
}
//33
if (aTPSPdocumentType33.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType33;
}
//34
if (aTPSPdocumentType34.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType34;
}
//35
if (aTPSPdocumentType35.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType35;
}
//41
if (aTPSPdocumentType41.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType41;
}
//42
if (aTPSPdocumentType42.equals(element.getNodeName())) {
aTPSPdocumentTypeX = aTPSPdocumentType42;
}
if ("atps:ReceivingSPadministrator".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:ReceivingSPadministrator//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
lastNodeString = element.getNodeName();
}
if ("spkt:SPadministratorId".equals(element.getNodeName())&(lastNodeString.equals("atps:ReceivingSPadministrator"))) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:ReceivingSPadministrator//spkt:SPadministratorId//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("atps:SendingSPadministrator".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:SendingSPadministrator//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
lastNodeString = element.getNodeName();
}
if ("spkt:SPadministratorId".equals(element.getNodeName())&(lastNodeString.equals("atps:SendingSPadministrator"))) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:SendingSPadministrator//spkt:SPadministratorId//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("atps:TransferToAccount".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:TransferToAccount//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("atps:ContributionAccount".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:ContributionAccount//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("atps:AccountOwner".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:AccountOwner//";
System.out.println("lastNode is cut to: " + lastNode);
lastNodeStringAccountOwner = "atps:AccountOwner";
System.out.println("\n");
}

if (("FogP:AccountOwnerId".equals(element.getNodeName())) & (lastNodeStringAccountOwner.equals("atps:AccountOwner"))) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:AccountOwner//FogP:AccountOwnerId//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if (("FogP:AccountOwnerName".equals(element.getNodeName())) & (lastNodeStringAccountOwner.equals("atps:AccountOwner"))) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps:AccountOwner//FogP:AccountOwnerName//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
//hertil
if ("atps vertakingCompany".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps vertakingCompany//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("spkt:AccountHoldingCompanyId".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps vertakingCompany//spkt:AccountHoldingCompanyId//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("spkt:AccountHoldingCompanyName".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps vertakingCompany//spkt:AccountHoldingCompanyName//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
if ("spkt:AccountHoldingCompanyAddress".equals(element.getNodeName())) {
lastNode = "atps:ATPSPsubmission//atps:ATPSPdocument//atps:mDocumentBody//" + aTPSPdocumentTypeX + "//atps vertakingCompany//spkt:AccountHoldingCompanyAddress//";
System.out.println("lastNode is cut to: " + lastNode);
System.out.println("\n");
}
hashMap.put((lastNode + element.getNodeName()),element.getFirstChild().getNodeValue());
System.out.println("HashMap nodename is \n" +
(lastNode + element.getNodeName()) + "\n" +
hashMap.get(lastNode + element.getNodeName()) + "\n" );
//System.out.println("\n");

}
System.out.print("atpDocumentBodyHit exit " + atpDocumentBodyHit);
System.out.println("hashMap in dok" + hashMap.toString());
arrayList.add(atpDocumentBodyHit++, hashMap);
hashMap = new HashMap();
System.out.println("arrayList in dok" + arrayList.get(0).toString());
}

catch (SAXException e) {
System.out.println("SAXException is: " + e.toString());
}
catch (IOException e) {
System.out.println("IOException is: " + e.toString());
}
hashMap = (HashMap) arrayList.get(1);
System.out.println("******************HashMap is: " + hashMap.toString());
return arrayList;
}
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I was thinking of something like
 
Tom Stevns
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bartender !



You are an excellent bartender



And I'm so stinking drunk.

Your method tagPathResolver works 95 %

Thanx a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic