當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript libxmljs.parseXml函數代碼示例

本文整理匯總了TypeScript中libxmljs.parseXml函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript parseXml函數的具體用法?TypeScript parseXml怎麽用?TypeScript parseXml使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了parseXml函數的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1:

import * as libxmljs from 'libxmljs';

const xml =  '<?xml version="1.0" encoding="UTF-8"?>' +
           '<root>' +
               '<child foo="bar">' +
                   '<grandchild baz="fizbuzz">grandchild content</grandchild>' +
               '</child>' +
               '<sibling>with content!</sibling>' +
           '</root>';

const xmlDoc = libxmljs.parseXml(xml);

// xpath queries
const gchild = xmlDoc.get('//grandchild')!;

console.log(gchild.text());  // prints "grandchild content"

const children = xmlDoc.root()!.childNodes();
const child = children[0] as libxmljs.Element;

console.log(child.attr('foo')!.value()); // prints "bar"

const parser = new libxmljs.SaxParser();

parser.on('startDocument', () => 0);
parser.on('startElement', () => 0);

const parser2 = new libxmljs.SaxPushParser();

// connect any callbacks here
parser2
開發者ID:pixelshaded,項目名稱:DefinitelyTyped,代碼行數:31,代碼來源:libxmljs-tests.ts

示例2: require

/// <reference path="../node/node.d.ts" />
/// <reference path="libxmljs.d.ts" />

var libxmljs = require("libxmljs");
var xml =  '<?xml version="1.0" encoding="UTF-8"?>' +
           '<root>' +
               '<child foo="bar">' +
                   '<grandchild baz="fizbuzz">grandchild content</grandchild>' +
               '</child>' +
               '<sibling>with content!</sibling>' +
           '</root>';

var xmlDoc = libxmljs.parseXml(xml);

// xpath queries
var gchild = xmlDoc.get('//grandchild');

console.log(gchild.text());  // prints "grandchild content"

var children = xmlDoc.root().childNodes();
var child = children[0];

console.log(child.attr('foo').value()); // prints "bar"

var parser = new libxmljs.SaxParser();

parser.on('startDocument', null);
parser.on('startElement', null);

var parser2 = new libxmljs.SaxPushParser();
開發者ID:Agamnentzar,項目名稱:DefinitelyTyped,代碼行數:30,代碼來源:libxmljs-tests.ts

示例3: request

export function request(
  data,
  cwmpVersion,
  warn
): {
  id: string;
  cwmpVersion: string;
  sessionTimeout: number;
  cpeRequest?: CpeRequest;
  cpeFault?: CpeFault;
  cpeResponse?: CpeResponse;
} {
  warnings = warn;

  const rpc = {
    id: null,
    cwmpVersion: cwmpVersion,
    sessionTimeout: null,
    cpeRequest: null,
    cpeFault: null,
    cpeResponse: null
  };

  if (!data.length) return rpc;

  const xml = parseXml(data, LIBXMLJS_PARSE_OPTIONS);

  let headerElement, bodyElement;

  for (const c of xml.childNodes()) {
    switch (c.name()) {
      case "Header":
        headerElement = c;
        break;
      case "Body":
        bodyElement = c;
        break;
    }
  }

  if (headerElement) {
    for (const c of headerElement.childNodes()) {
      switch (c.name()) {
        case "ID":
          rpc.id = c.text();
          break;
        case "sessionTimeout":
          rpc.sessionTimeout = parseInt(c.text());
          break;
      }
    }
  }

  for (const methodElement of bodyElement.childNodes()) {
    const methodName = methodElement.name();
    if (methodName === "text") continue;

    if (!rpc.cwmpVersion && methodName !== "Fault") {
      switch (methodElement.namespace().href()) {
        case "urn:dslforum-org:cwmp-1-0":
          rpc.cwmpVersion = "1.0";
          break;
        case "urn:dslforum-org:cwmp-1-1":
          rpc.cwmpVersion = "1.1";
          break;
        case "urn:dslforum-org:cwmp-1-2":
          if (rpc.sessionTimeout) rpc.cwmpVersion = "1.3";
          else rpc.cwmpVersion = "1.2";

          break;
        case "urn:dslforum-org:cwmp-1-3":
          rpc.cwmpVersion = "1.4";
          break;
        default:
          throw new Error("Unrecognized CWMP version");
      }
    }

    switch (methodName) {
      case "Inform":
        rpc.cpeRequest = Inform(methodElement);
        break;
      case "GetRPCMethods":
        rpc.cpeRequest = GetRPCMethods();
        break;
      case "TransferComplete":
        rpc.cpeRequest = TransferComplete(methodElement);
        break;
      case "RequestDownload":
        rpc.cpeRequest = RequestDownload(methodElement);
        break;
      case "GetParameterNamesResponse":
        rpc.cpeResponse = GetParameterNamesResponse(methodElement);
        break;
      case "GetParameterValuesResponse":
        rpc.cpeResponse = GetParameterValuesResponse(methodElement);
        break;
      case "SetParameterValuesResponse":
        rpc.cpeResponse = SetParameterValuesResponse(methodElement);
        break;
//.........這裏部分代碼省略.........
開發者ID:zaidka,項目名稱:genieacs,代碼行數:101,代碼來源:soap.ts


注:本文中的libxmljs.parseXml函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。