Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

379
Vistas
XML Null when getting child node

Null results when trying to access children of the first item. I'm using Google App Scripts.

function parseXml() {
  var url = 'https://example.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();

  var channel = root.getChild('channel');
  var items = channel.getChildren('item');
  Logger.log(items[1].getValue());
  Logger.log(items[1].getChildren())
  Logger.log(items[1].getChild('g:id'))

}

Output:

5:37:46 PM  Notice  Execution started
5:37:57 PM  Info    09771332150202100001A&C Ainsworth Wines - A&C Ainsworth Wines - 2021
5:37:57 PM  Info    [[Element: <g:id [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:title [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:description [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:link [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:image_link [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:availability [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:price [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:item_group_id [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:brand [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:gender [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:condition [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:google_product_category [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:product_type [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:spec [Namespace: http://base.google.com/ns/1.0]/>], [Element: <quantity/>], [Element: <option_values/>]]
5:37:57 PM  Info    null
5:37:58 PM  Notice  Execution completed

Finally, XML example. Sorry, I can't share the source URL.

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
 <item>
  <g:id>09771332140202100001</g:id>
   <g:title>A&C Ainsworth Wines - A&C Ainsworth Wines - 2021</g:title>
   <g:description></g:description>
 </item>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe your current issue and your goal are as follows.

  • Logger.log(items[1].getChild('g:id')) returns null.
  • You want to retrieve the value from <g:id>###</g:id> in item by modifying your script.

When I saw your showing XML and your script, in your script, I thought that the namespace is required to be used. When this is reflected in your script, it becomes as follows.

From:

Logger.log(items[1].getChild('g:id'))

To:

Logger.log(items[1].getChild('id', XmlService.getNamespace("http://base.google.com/ns/1.0")))

or

Logger.log(items[1].getChild('id', XmlService.getNamespace("g", "http://base.google.com/ns/1.0")))

or

Logger.log(items[1].getChild('id', XmlService.getNamespace("http://base.google.com/ns/1.0")).getValue())

or

Logger.log(items[1].getChild('id', XmlService.getNamespace("g", "http://base.google.com/ns/1.0")).getValue())
  • Although I'm not sure about your actual XML data and I cannot test this, I thought that in the case of your XML data, Logger.log(items[1].getChild('id', XmlService.getNamespace("http://base.google.com/ns/1.0")).getValue()) might work.

Reference:

  • getNamespace(uri)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda