如何检查xml元素是否具有子元素 [英] How to check if a xml element has a children element

查看:75
本文介绍了如何检查xml元素是否具有子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我读一个xml文件有点问题,首先我从webservice下载一个xml文件,xml非常dinamics





这是一个小例子

 <   >  
< 产品 ID = 1 >
< Product_id > 1 < / Product_id >
< Product_name > 产品1 < / Product_name >
< Product_price / >
< / Product >
< 产品 ID = 2 >
< Product_id > 2 < / Product_id >
< Product_name > 产品2 < / Product_name >
< Product_price > 2000 < / Product_price >
< 描述 >
< 日期 > 25-10-2015 < /日期 > < / Description >
< 数量 > 45 < / Quantity < span class =code-keyword>>
< / Product >
< 产品 ID = 3 >
< < span class =code-leadattribute> Product_id > 3 < / Product_id >
< Product_name > 产品3 < / Product_name >
< Product_price > 3000 < / Product_price >
< / Product >
< 产品 ID = 4 >
< Product_id
> 4 < / Product_id >
< Product_name > 产品4 < / Product_name >
< Product_price > 4000 < / Product_price >
< / Product >
< /表 >







元素描述里面有元素,我的问题是我可以知道所说的元素是否是一个将其他元素汇集在一起​​的元素。



我正在尝试使用Visual Basic。



如果有人知道请告诉我。


谢谢。

解决方案

 <  说明 >  
< < span class =code-leadattribute>日期 > 25-10-2015 < / Description >



First of您的所有日期标记格式错误,结束标记丢失。



问题很明显。你知道一个元素有子元素,因为下一个标记不是元素的结束标记。



阅读< product>之后标签,每个不是< / product>的标签是一个子元素。


忘记元素Date,是示例的一部分。

逻辑显示元素描述内部有更多行。


以下是示例,在第一个代码中,您可以看到第四个LineasFactSinOc没有子节点。



 <   ICDoc    版本  =  4.01 >  
< FechaXML > 2014-10-15T12:00:09 < / FechaXML >
< Documento ID = 6549657 >
< LineasFactSinOc / >
< Encabezado >
< IdDoc >
< TipoDoc > FC < / TipoDoc >
< NumDoc > 1020065 < / NumDoc >
< FolioUnico > 0101-382411 < / F olioUnico >
< FchTransaccion > 2014-05-30T18:53:26 < / FchTransaccion >
< < span class =code-leadattribute> FchEmis > 2014-05-14 < / FchEmis >
< FchVenc > 2014-06-13 < ; / FchVenc >
< FchRecepcion > 2014-05-19 < / FchRecepcion >
< FchEstimadaPago > 2014-06-06 < / FchEstimadaPago >
< Comentarios > Nombre Usuario:MVENEGASN Numero Lote:138621 < / Comentarios >
< TipoDocTributario < span class =code-keyword>> Exenta < / TipoDocTributario >
< AgenteRetenedor > 1 < / AgenteRetenedor >
< SinOC > false < / SinOC >
< Electronico > false < / Electronico >
< Estado > ... < / Estado >
< EstadoAsoc > ... < / EstadoAsoc >
< MonedaRef > ... < / MonedaRef >
< TipoDocFactura > ... < / TipoDocFactura >
< / IdDoc >
&l t; Comprador > ... < / Comprador >
< Proveedor > ... < / Proveedor >
< Totales > ... < / Totales >
< / Encabezado >
< DocAsociados ID = 9475465 > ... < / DocAsociados >
< Cuadre ID = 1283873 > ... < < span class =code-leadattribute> / Cuadre >
< / Documento >
< / ICDoc >





但是是它的一部分。子节点和这两个代码属于同一个下载的xml文件。



如果你看到有更多节点null



我的问题就是例如。节点LineasFactSinOc如何检查内部是否有更多节点,如果是否为空。



如果我没有错,那么有更多的节点为空。



 <   ICDoc     version   =  4.01 >  
< < span class =code-leadattribute> FechaXML > 2014-10-15T12:00:09 < ; / FechaXML >
< Documento ID = 6549697 >
< LineasFactSinOc < span class =code-keyword>>
< LineaFactSinOc >
< NUMLINEA > 1 < / NUMLINEA >
< IDMSTRITEM > 0 < / IDMSTRITEM >
< IDARTICULO > C.06.01.04.016 < / IDARTICULO >
< NOMBARTICULO > Petroleo Diesel < ; / NOMBARTICULO >
< CANTIDAD > 639.0000 < ; / CANTIDAD >
< IDUOM > LTR < ; / IDUOM >
< IDMONEDA / >
< PRECIOUNITARIO > 529.800000 < / PRECIOUNITARIO >
< COMENTARIOS / >
< GLOSA / < span class =code-keyword>>
< TASACAMBIO > 1.000000 < / TASACAMBIO < span class =code-keyword>>
< TOTALLINEA > 338542.200000 < / TOTALLINEA >
< IDLINEA > 369047 < / IDLINEA >
< DistribucionFactSinOc >
< IDCENTCOSTO > 00609 < / IDCENTCOSTO >
< TIPODISTRIB > 1 < / TIPODISTRIB >
< VALOR > 100.0000 < / VALOR >
< IDLINEADOC > 369047 < / IDLINEADOC >
< / DistribucionFactSinOc >
< / LineaFactSinOc >
< / LineasFactSinOc >
< Encabezado >
< span class =code-keyword>< IdDoc >
< TipoDoc > FC < / TipoDoc >
< NumDoc > 2441397 < / NumDoc >
< FolioUnico > 0101-382435 < / FolioUnico >
< FchTransaccion < span class =code-keyword>> 2014-05-30T17:14:30 < / FchTransaccion >
< FchEmis > 2014-05-15 < / FchEmis >
< FchVenc > 2014-06-14 < / FchVenc >
< FchRecepcion > 2014-05-26 < / FchRecepcion >
< FchEstimadaPago > 2014-06-06 < span class =code-keyword>< / FchEstimadaPago >
< Comentarios > Nombre Usuario :IROJASV Numero Lote:138634 < / Comentarios >
< TipoDocTributario > ; Afecta < / TipoDocTributario >
< AgenteRetenedor > 1 < / AgenteRetenedor >
< SinOC > true < / SinOC >
< Electronico > true < / Electronico >
< Estado > ... < / Estado >
< EstadoAsoc > ... < / EstadoAsoc >
< MonedaRef > ... < / MonedaRef >
< TipoDocFactura > ... < / TipoDocFactura < span class =code-keyword>>
< / IdDoc >
< Comprador > ... < / Comprador >
< Proveedor > ... < / Proveedor >
< Totales > ... < / Totales >
< < span class =code-leadattribute> / Encabezado >
< OtrosImpuestos > ... < / OtrosImpuestos >
< CuentasCosto > ... < / CuentasCosto >
< span class =code-keyword>< Cuadre ID = 1283783 > ... < / Cuadre >
< / Documento >
< / ICDoc >





我将下一个代码视觉化基本



  reader.read()

选择 案例 XmlNodeType.Element

MsgBox( 元素名称 + reader.name)

选择 案例 XmlNodeType.Text


MsgBox( 值元素: + reader.value)

选择 < span class =code-keyword> Case XmlEndElement

MsgBox( 要关闭的节点名称 + reader.name)

退出 选择

结束 虽然


Hi everyone.

I have a little problem to read a xml file, first I download a xml file from a webservice, the xml is very dinamics


this is little example

<Table>
<Product ID="1">
<Product_id>1</Product_id>
<Product_name>Product 1</Product_name>
<Product_price/>
</Product>
<Product ID="2">
<Product_id>2</Product_id>
<Product_name>Product 2</Product_name>
<Product_price>2000</Product_price>
<Description>
    <Date>25-10-2015</Date></Description>
    <Quantity>45</Quantity>
</Product>
<Product ID="3">
<Product_id>3</Product_id>
<Product_name>Product 3</Product_name>
<Product_price>3000</Product_price>
</Product>
<Product ID="4">
<Product_id>4</Product_id>
<Product_name>Product 4</Product_name>
<Product_price>4000</Product_price>
</Product>
</Table>




In the element "Description" has element inside, my question is I can know if that said element is an element which brings together other elements inside.

I'm trying to make it on visual basic.

Please if someone knows let me know it.

Thanks.

解决方案

<Description>
    <Date>25-10-2015</Description>


First of all your Date tag is malformed the end tag is missing.

Question is obvious. you know that an element have children because the next tag is not the closing tag of the element.

After you read <product> tag, every tag that is not </product> is a children element.


Forget the element Date, is part of example.
The logic is show the element Description have more line inside.


Here are the examples, in the first code you can see the fourth LineasFactSinOc not have child nodes.

<ICDoc version="4.01">
<FechaXML>2014-10-15T12:00:09</FechaXML>
<Documento ID="6549657">
<LineasFactSinOc/>
<Encabezado>
<IdDoc>
<TipoDoc>FC</TipoDoc>
<NumDoc>1020065</NumDoc>
<FolioUnico>0101-382411</FolioUnico>
<FchTransaccion>2014-05-30T18:53:26</FchTransaccion>
<FchEmis>2014-05-14</FchEmis>
<FchVenc>2014-06-13</FchVenc>
<FchRecepcion>2014-05-19</FchRecepcion>
<FchEstimadaPago>2014-06-06</FchEstimadaPago>
<Comentarios>Nombre Usuario:MVENEGASN Numero Lote:138621</Comentarios>
<TipoDocTributario>Exenta</TipoDocTributario>
<AgenteRetenedor>1</AgenteRetenedor>
<SinOC>false</SinOC>
<Electronico>false</Electronico>
<Estado>...</Estado>
<EstadoAsoc>...</EstadoAsoc>
<MonedaRef>...</MonedaRef>
<TipoDocFactura>...</TipoDocFactura>
</IdDoc>
<Comprador>...</Comprador>
<Proveedor>...</Proveedor>
<Totales>...</Totales>
</Encabezado>
<DocAsociados ID="9475465">...</DocAsociados>
<Cuadre ID="1283873">...</Cuadre>
</Documento>
</ICDoc>



But on this part it have. child node and this both code belong to the same xml file downloaded.

If you see too there is more node null

My question is for example. the node "LineasFactSinOc" how can check if have more node inside and if is null or not.

if I not get wrong there is more nodes nulls.

<ICDoc version="4.01">
<FechaXML>2014-10-15T12:00:09</FechaXML>
<Documento ID="6549697">
<LineasFactSinOc>
<LineaFactSinOc>
	<NUMLINEA>1</NUMLINEA>
	<IDMSTRITEM>0</IDMSTRITEM>
	<IDARTICULO>C.06.01.04.016</IDARTICULO>
	<NOMBARTICULO>Petroleo Diesel</NOMBARTICULO>
	<CANTIDAD>639.0000</CANTIDAD>
	<IDUOM>LTR</IDUOM>
	<IDMONEDA/>
	<PRECIOUNITARIO>529.800000</PRECIOUNITARIO>
	<COMENTARIOS/>
	<GLOSA/>
	<TASACAMBIO>1.000000</TASACAMBIO>
	<TOTALLINEA>338542.200000</TOTALLINEA>
	<IDLINEA>369047</IDLINEA>
	<DistribucionFactSinOc>
		<IDCENTCOSTO>00609</IDCENTCOSTO>
		<TIPODISTRIB>1</TIPODISTRIB>
		<VALOR>100.0000</VALOR>
		<IDLINEADOC>369047</IDLINEADOC>
	</DistribucionFactSinOc>
</LineaFactSinOc>
</LineasFactSinOc>
<Encabezado>
<IdDoc>
<TipoDoc>FC</TipoDoc>
<NumDoc>2441397</NumDoc>
<FolioUnico>0101-382435</FolioUnico>
<FchTransaccion>2014-05-30T17:14:30</FchTransaccion>
<FchEmis>2014-05-15</FchEmis>
<FchVenc>2014-06-14</FchVenc>
<FchRecepcion>2014-05-26</FchRecepcion>
<FchEstimadaPago>2014-06-06</FchEstimadaPago>
<Comentarios>Nombre Usuario:IROJASV Numero Lote:138634</Comentarios>
<TipoDocTributario>Afecta</TipoDocTributario>
<AgenteRetenedor>1</AgenteRetenedor>
<SinOC>true</SinOC>
<Electronico>true</Electronico>
<Estado>...</Estado>
<EstadoAsoc>...</EstadoAsoc>
<MonedaRef>...</MonedaRef>
<TipoDocFactura>...</TipoDocFactura>
</IdDoc>
<Comprador>...</Comprador>
<Proveedor>...</Proveedor>
<Totales>...</Totales>
</Encabezado>
<OtrosImpuestos>...</OtrosImpuestos>
<CuentasCosto>...</CuentasCosto>
<Cuadre ID="1283783">...</Cuadre>
</Documento>
</ICDoc>



I made the next code visual basic

while reader.read()

       Select Case  XmlNodeType.Element
            
           MsgBox("Element name"+reader.name)
              
      Select Case XmlNodeType.Text

          
           MsgBox("Value Element:"+reader.value)

     Select Case XmlEndElement

           MsgBox("Node Name to Close"+reader.name)

     Exit Select

End While


这篇关于如何检查xml元素是否具有子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆