如何将子节点读入另一个节点?

时间:2019-05-21 16:32:07

标签: xml asp-classic msxml6

需要从XML文件(格式和Excel.Sheet xmlns)中读取子节点,但要使用索引属性而不是子编号

我们检查是否使用带有MSXML2.FreeThreadedDOMDocument的对象读取类似于RSS / XML文件的内容。所以最后我们用MSXML2.DOMDocument.6.0解决了连接器 然后将一些属性设置为语言和命名空间。有了它,可以直接读取一个objXML.selectedNodes,用objXML.Length知道多少项,然后使用一个简单的For Each Node读取单个组。 然后使用Node.ChilNodes(nn).text检索需要处理的数据

要读取的XML数据示例

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office">
 <Styles>...</Styles>
  <Worksheet ss:Name="movimientos">
   <Table>
    <Row ss:Index="1">
     <Cell ss:Index="1" ss:StyleID="s1">
      <Data ss:Type="String">Fecha operación</Data>
     </Cell>
     <Cell ss:Index="2" ss:StyleID="s1">
      <Data ss:Type="String">Fecha valoración</Data>
     </Cell>
     <Cell ss:Index="3" ss:StyleID="s1">
      <Data ss:Type="String">Cod. oper.</Data>
     </Cell>
     <Cell ss:Index="4" ss:StyleID="s1">
      <Data ss:Type="String">Concepto</Data>
     </Cell>
     <Cell ss:Index="5" ss:StyleID="s1">
      <Data ss:Type="String">Importe</Data>
     </Cell>
     <Cell ss:Index="6" ss:StyleID="s1">
      <Data ss:Type="String">Saldo</Data>
     </Cell>
     <Cell ss:Index="7" ss:StyleID="s1">
       <Data ss:Type="String">N. Documento</Data>
     </Cell>
     <Cell ss:Index="8" ss:StyleID="s1">
      <Data ss:Type="String">Referencia 1</Data>
     </Cell>
     <Cell ss:Index="9" ss:StyleID="s1">
      <Data ss:Type="String">Referencia 2</Data>
     </Cell>
</Row>

等...

使用它在ASP Classic中阅读

    Set oXMLDoc = Server.CreateObject("MSXML2.DOMDocument.6.0")
oXMLDoc.setProperty "SelectionLanguage", "XPath"
oXMLDoc.setProperty "SelectionNamespaces", "xmlns:myns='urn:schemas-microsoft-com:office:spreadsheet' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet' xmlns:html='http://www.w3.org/TR/REC-html40' xmlns:msxsl='urn:schemas-microsoft-com:xslt' xmlns:user='urn:my-scripts'"
oXMLDoc.async = False
oXMLDoc.setProperty "ServerHTTPRequest", True
oXMLDoc.Load  Server.MapPath("/bsch.xls")
Set oXMLRows = oXMLDoc.selectNodes("/myns:Workbook/myns:Worksheet/myns:Table/myns:Row")
For Each Node In oXMLRows
 Response.Write "Entries=" & Node.childNodes.length
 Response.Write "" & Node.childNodes(0).text & "<br/>" & Node.childNodes(1).text & Node.childNodes(2).text & Node.childNodes(3).text  & Node.childNodes(4).text & Node.childNodes(5).text & Node.childNodes(6).text & Node.childNodes(7).text & Node.childNodes(8).text
 Response.Write "FULL STRING CONTENT =" & Node.text & "<br/>"
Next

更改实际值:

Node.childNodes(n).text 

读取每个孩子,使用INDEX属性读取,例如 Node.ChildNodes("@index='1'").text(实际上不运行)

任何具有有关XML和ASP“如何使用”文档的URL都会感激

0 个答案:

没有答案