开发者

Parsing XML with elements containing colon / namespace

开发者 https://www.devze.com 2023-01-27 01:05 出处:网络
Using Xml to parse an XML document in Google Apps Script : http://code.google.com/googleapps/appsscript/articles/XML_tutorial.html#HowItWorks

Using Xml to parse an XML document in Google Apps Script : http://code.google.com/googleapps/appsscript/articles/XML_tutorial.html#HowItWorks

But this doesn't work (parse fails) if there is a colon in the element name. Even though it maybe the nam开发者_如何学Pythonespace, its a single namespace throughout the XML document.

<aws:elementname>...</aws:elementname>

Is this is an issue only with the google's Xml or is it generic ?


Just don't send argument as true.

var oXML = Xml.parse(sXML, false);
var root = oXML.getElement();
var topElement = root.getElements("http://namespace-uri","topElement");
var childElement = topElement[0].getElements("http://namespace-uri","childElement");


It is definitely not a general issue. There certainly are XML parsers that handle namespaces.

I suspect it's a limitation of the tutorial code and that the google libraries actually can handle namespaces, but it's somewhat guesswork from looking at the API docs.

The tutorial code is using calls like

var movies = doc.html.head.getElements("movie");

which seems to be a non-namespace-aware version.

There is an overload of this method that takes a namespace URL as well, and which you might need to use if there's a namespace involved.


If you want to use some namespace, you have to first declare it. E.g.:

<root xmlns:aws="some-uri">
  <aws:elementname />
</root>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号