This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes How to get the XML content and attributes bet ween XML elements using Regex and Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get the XML content and attributes bet ween XML elements using Regex and Pattern" Watch "How to get the XML content and attributes bet ween XML elements using Regex and Pattern" New topic
Author

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

Venkat Nag
Greenhorn

Joined: Feb 12, 2013
Posts: 1
<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

Joined: Jan 28, 2008
Posts: 5575

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
 
Don't get me started about those stupid light bulbs.
 
subject: How to get the XML content and attributes bet ween XML elements using Regex and Pattern