-1

My xml response that comes from ajax is

<gml:Polygon srsName="http://www.opengis.net/gml/srs/epsg.xml#3857">
  <gml:outerBoundaryIs>
    <gml:LinearRing>
       <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="," ts=" ">33.36,49.79 33.7410766436,49.788 33.17,49.09 33.62,49.16 33.07,49.46 33.36,49.79</gml:coordinates>
    </gml:LinearRing>
  </gml:outerBoundaryIs>
</gml:Polygon>

And I want to get coordinates in <gml:coordinates> node.

Can I get the coordinate values using regex or any other way?

3
  • 1
    Have you tried this approach: stackoverflow.com/questions/5415452/…. Using regex can be difficult to maintain. Maybe this: stackoverflow.com/questions/7949752/… Commented Oct 5, 2020 at 7:41
  • 3
    Do not use regex! Use xml parser. Commented Oct 5, 2020 at 7:50
  • XML is not meant to be parsed via RegEx. While it, physically, is a text file, this text is more to be seen as source code, that describes a tree. This is why one should use an XML parser, which then allows access to the in-memory tree via different means. However, sometimes it is just overdose to use a parser and small, quick tasks could be done via RegEx. A few days ago I found a paper on the web, which describes a Shallow XML Parser via regex. Maybe that is of interest to you. Commented Oct 7, 2020 at 1:39

1 Answer 1

1
var text = '<gml:Polygon srsName="http://www.opengis.net/gml/srs/epsg.xml#3857">'
+ '<gml:outerBoundaryIs>'
+ '<gml:LinearRing>'
+ '<gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="," ts=" ">33.36,49.79 33.7410766436,49.788 33.17,49.09 33.62,49.16 33.07,49.46 33.36,49.79</gml:coordinates>'
+ '</gml:LinearRing>'
+ '</gml:outerBoundaryIs>'
+ '</gml:Polygon>';

// parse xml
var parser = new DOMParser();
var doc = parser.parseFromString(text,"text/xml");
// extract node using XPath
var node = doc.evaluate('//*[local-name()="coordinates"]', doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
// get node text
console.log(node.textContent);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.