Parsing XML with elements containing colon / namespace

烂漫一生 提交于 2019-12-25 01:47:12

问题


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 namespace, 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 ?


回答1:


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");



回答2:


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.




回答3:


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

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


来源:https://stackoverflow.com/questions/4293894/parsing-xml-with-elements-containing-colon-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!