• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get the XML content and attributes bet ween XML elements using Regex and Pattern

 
Venkat Nag
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<ns5:ConfigResponseList xmlns:ns5="http://www.cisco.com/licensing/api/GetPakConfig">
- <ns5:ConfigResponse PakCode="ABC123456">
- <ns5:Status>
<ns6:StatusId xmlns:ns6="http://www.cisco.com/licensing/api/Status">ERROR</ns6:StatusId>
- <ns7:Message xmlns:ns7="http://www.cisco.com/licensing/api/Status" id="ERR_INVALID_PAK" locale="en_US">
<ns7:MessageDescription>FR-version:Invalid PAK or error in reading PAK Configuration : Veuillez s'il vous plait v??rifiez si le PAK ABC123456 est entr?? correctement. Si vous continuez ?? recevoir ce message apr??s v??rification, nous vous prions d'ouvrir une demande en utilisant l'outil de service du TAC http://tools.cisco.com/ServiceRequestTool/create/DefineProblem.do ou d'envoyer un courriel ?? licensing@cisco.com. Veuillez svp avoir votre num??ro d'utilisateur et mot de passe Cisco disponible. Comme alternative, vous pouvez ??galement appeler notre centre d'assistance technique au 800-553-2447.</ns7:MessageDescription>
</ns7:Message>
</ns5:Status>
</ns5:ConfigResponse>
- <ns5:ConfigResponse PakCode="FXPAK1E5D80">
- <ns5:Status>
<ns8:StatusId xmlns:ns8="http://www.cisco.com/licensing/api/Status">ERROR</ns8:StatusId>
- <ns9:Message xmlns:ns9="http://www.cisco.com/licensing/api/Status" id="ERR_PAK_INVALIDATED" locale="en_US">
<ns9:MessageDescription>FR-version:The PAK FXPAK1E5D80 seems to be invalidated in the system.Veuillez s'il vous plait v??rifiez si le PAK FXPAK1E5D80 est entr?? correctement. Si vous continuez ?? recevoir ce message apr??s v??rification, nous vous prions d'ouvrir une demande en utilisant l'outil de service du TAC http://tools.cisco.com/ServiceRequestTool/create/DefineProblem.do ou d'envoyer un courriel ?? licensing@cisco.com. Veuillez svp avoir votre num??ro d'utilisateur et mot de passe Cisco disponible. Comme alternative, vous pouvez ??galement appeler notre centre d'assistance technique au 800-553-2447.</ns9:MessageDescription>
</ns9:Message>
</ns5:Status>
</ns5:ConfigResponse>
- <ns5:ConfigResponse PakCode="FXPAKF7CE82">
- <ns5:Status>
<ns10:StatusId xmlns:ns10="http://www.cisco.com/licensing/api/Status">ERROR</ns10:StatusId>
- <ns11:Message xmlns:ns11="http://www.cisco.com/licensing/api/Status" id="ERR_PAK_RMA" locale="en_US">
<ns11:MessageDescription>FR-version:RMA of PAK:FXPAKF7CE82 has already been done.If you are still receiving this message after verification then please open a Service Request using the TAC Service Request Tool,'http://tools.cisco.com/ServiceRequestTool/create/DefineProblem.do' or send an email to licensing@cisco.com for next steps. Please have your valid Cisco.com user Id and password available. As an alternative, you may also call our main Technical Assistance Center at 800-553-2447.</ns11:MessageDescription>
</ns11:Message>
</ns5:Status>
</ns5:ConfigResponse>
</ns5:ConfigResponseList>

My XML is dynamic..so i want to try using java pattrens and regex

Pattern tagPattern = Pattern.compile("<ns5:ConfigResponseList xmlns:ns5='http://www.cisco.com/licensing/api/GetPakConfig'>(.+?)</ns5:ConfigResponseList>");
Pattern attValueDoubleQuoteOnly = Pattern.compile("(\\w+)=\"(.*?)\"");
Pattern attValueAll = Pattern.compile("([\\w:\\-]+)(\\s*=\\s*(\"(.*?)\"|'(.*?)'|([^ ]*))|(\\s+|\\z))");
Matcher m = tagPattern.matcher(testHtml);
boolean tagFound = m.find(); // true
String tagOnly = m.group(0);// <tag a ="b" c= 'd' e=f> contentssss </tag>
String attributes = m.group(1);// tag
// String attributes = m.group(2);// a ="b" c= 'd' e=f
// String content = m.group(3);// contentssss
System.out.println("Tag Only : " + tagOnly);
System.out.println("Attributes : " + attributes);
// System.out.println("Attributes : " + attributes);
// System.out.println("Content : " + content);
//m = attValueDoubleQuoteOnly.matcher(attributes);
m = attValueAll.matcher(attributes);
while (m.find()) {
System.out.println(" >> " + m.group(0));
}

Using the above code i am not getting the MessageDescription & Status ID contents?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Venkat Nag

why cant you use XPath with DOM parser, also it is included in java 1.6 (cant remember exact version) . it is simple and easy to use.

Of course it is a suggestion, someone around here may help you better in regular expression way
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic