Reputation: 33
I have a java string that contains the following XML code:
<?xml version="1.0" encoding="utf-8"?>
<Chart>
<request>
<zip>12345</zip>
<city>Miami</city>
</request>
</Chart>
What is the easiest way to parse this string to extract the value of
<zip> (in this case 12345)
Upvotes: 1
Views: 1125
Reputation: 3079
You have XML, better is parse it as XML, and XPATH direct
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
String xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
" <Chart>\r\n" +
" <request>\r\n" +
" <zip>12345</zip>\r\n" +
" <city>Miami</city>\r\n" +
" </request>\r\n" +
" </Chart>";
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// PARSE XML
Document document = builder.parse(new InputSource(new StringReader(xml)));
// XPATH
XPath xPath = XPathFactory.newInstance().newXPath();
// your path
String expression = "//Chart/request/zip";
NodeList nodes = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for(int i=0; i<nodes.getLength(); i++)
{
Node the_node = nodes.item(i);
if(the_node instanceof Element)
{
Element the_element=(Element) the_node;
System.out.println("element="+the_element.getTextContent());
break; // STOP at the first
}
}
Upvotes: 1
Reputation: 9586
Without going into the deep dark world of parsing xml with Java, you could use regex:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class FindZip {
public static void main(String[] args) {
Pattern pattern =
Pattern.compile("<zip>(\\d+)</zip>");
String zip_code;
Matcher matcher = pattern.matcher(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Chart>" +
" <request>" +
" <zip>12345</zip>" +
" <city>Miami</city>" +
" </request>" +
"</Chart>"
);
boolean found = false;
while (matcher.find()) {
zip_code = matcher.group(1);
System.out.printf(
"I found the zip code \"%s\" starting at index %d and ending at index %d.%n",
zip_code,
matcher.start(1),
matcher.end(1)
);
found = true;
}
if (!found) {
System.out.println("No match found.");
}
}
}
There are obvious drawbacks and limitations to this approach, but at least you get your zip code
Upvotes: 0