検索
ホームページバックエンド開発XML/RSS チュートリアルXML ドキュメントに新しい「レコード」を追加する

XML ドキュメントに新しい「レコード」を追加する

Feb 11, 2017 pm 03:54 PM
recordsXMLドキュメント

この記事で示されている例は、HTML 形式のデータを XML に保存することに似ています。以前は、フォームが送信されると、通常は新しいドキュメントを作成していましたが、現在はドキュメントが既に存在する限り、直接追加できます。この手法の使用は、基本データの作成に似ています。

前回の記事ではXMLDOMの使い方を説明しました。したがって、この記事の例にすぐに入ることができます。

最初に考慮する必要があるのは、新しい「レコード」を追加するために使用する HTML フォームです。 「HTML フォーム データを XML に保存」の例では、このフォームをすでに使用しており、ファイル名を変更しただけですが、コードは同じです。

AddContact.html:

<html> 
  <head> 
   <title> Contact Information </title> 
  </head> 
  <body> 
   <form action="processAdd.asp" method="post">  
   <h3 id="Enter-nbsp-your-nbsp-contact-nbsp-information">Enter your contact information</h3>  
   First Name:  
   <input type="text" id="firstName" name="firstName"><br> Last Name:  
   <input type="text" id="lastName" name="lastName"><br> Address #1:  
   <input type="text" id="address1" name="address1"><br> Address #2:  
   <input type="text" id="address2" name="address2"><br> Phone Number:  
   <input type="text" id="phone" name="phone"><br> E-Mail:  
   <input type="text" id="email" name="email"><br>  
   <input type="submit" id="btnSub" name="btnSub" value="Submit"><br>  
   </form> 
  </body> 
  </html>


ADDを処理するためにこのHTMLフォームを設定しました。 ASP。ここの ASP ページには、XML ファイルと ROLODEX.XML が存在するかどうかを検出する機能があります。ファイルが存在する場合、ASP は新しいエントリをファイルに追加します。ファイルが存在しない場合は、ファイルを作成する必要があります。

Process Add.asp:

<% 
   &#39;-------------------------------------------------------------------- 
   &#39;The "addNewContacttoXML" Function accepts two parameters. 
   &#39;strXMLFilePath - The physical path where the XML file will be saved. 
   &#39;strFileName - The name of the XML file that will be saved. 
   &#39;-------------------------------------------------------------------- 
   Function addNewContacttoXML(strXMLFilePath, strFileName)  
    &#39;Declare local variables.  
    Dim objDom  
    Dim objRoot  
    Dim objRecord  
    Dim objField 
    Dim objFieldValue  
    Dim objattID  
    Dim objattTabOrder  
    Dim objPI  
    Dim blnFileExists  
    Dim x  
    &#39;Instantiate the Microsoft XMLDOM.  
    Set objDom = server.CreateObject("Microsoft.XMLDOM")  
    objDom.preserveWhiteSpace = True 
    &#39;Call the Load Method of the XMLDOM Object. The Load ethod has a  
    &#39;boolean return value indicating whether or not the file could be  
    &#39;loaded. If the file exists and loads it will return true, otherwise, 
    &#39;it will return false. 
    blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)  
    &#39;Test to see if the file loaded successfully.  
    If blnFileExists = True Then  
     &#39;If the file loaded set the objRoot Object equal to the root element  
     &#39;of the XML document.  
     Set objRoot = objDom.documentElement Else  
     &#39;Create your root element and append it to the XML document.  
     Set objRoot = objDom.createElement("rolodex")  
     objDom.appendChild objRoot 
    End If  
     &#39;Create the new container element for the new record.  
     Set objRecord = objDom.createElement("contact")  
     objRoot.appendChild objRecord  
     &#39;Iterate through the Form Collection of the Request Object. 
     For x = 1 To Request.Form.Count  
      &#39;Check to see if "btn" is in the name of the form element. If it is,  
      &#39;then it is a button and we do not want to add it to the XML  
      &#39;document".  
      If instr(1,Request.Form.Key(x),"btn") = 0 Then  
       &#39;Create an element, "field".  
       Set objField = objDom.createElement("field")  
       &#39;Create an attribute, "id".  
       Set objattID = objDom.createAttribute("id")  
       &#39;Set the value of the id attribute equal the the name of the current  
       &#39;form field.  
       objattID.Text = Request.Form.Key(x)  
       &#39;The setAttributeNode method will append the id attribute to the  
       &#39;field element. objField.setAttributeNode objattID  
       &#39;Create another attribute, "taborder". This just orders the  
       &#39;elements.  
       Set objattTabOrder = objDom.createAttribute("taborder")  
        
       &#39;Set the value of the taborder attribute.  
       objattTabOrder.Text = x  
       &#39;Append the taborder attribute to the field element.  
       &#39;objField.setAttributeNode objattTabOrder  
       &#39;Create a new element, "field_value". 
       Set objFieldValue = objDom.createElement("field_value")  
       &#39;Set the value of the field_value element equal to the value of the  
       &#39;current field in the Form Collection.  
       objFieldValue.Text = Request.Form(x)  
       &#39;Append the field element as a child of the new record container  
       &#39;element, contact. objRecord.appendChild objField  
       &#39;Append the field_value element as a child of the field element. 
       objField.appendChild objFieldValue  
      End If  
     Next  
     &#39;Check once again to see if the file loaded successfully. If it did  
     &#39;not, that means we are creating a new document and need to be sure to  
     &#39;insert the XML processing instruction.  
     If blnFileExists = False then  
      &#39;Create the xml processing instruction.  
      Set objPI = objDom.createProcessingInstruction("xml", "version=&#39;1.0&#39;")  
      &#39;Append the processing instruction to the XML document.  
      objDom.insertBefore objPI, objDom.childNodes(0)  
     End If  
     &#39;Save the XML document. 
     objDom.save strXMLFilePath & "\" & strFileName  
     &#39;Release all of your object references.  
     Set objDom = Nothing  
     Set objRoot = Nothing  
     Set objRecord = Nothing  
     Set objField = Nothing  
     Set objFieldValue = Nothing  
     Set objattID = Nothing  
     Set objattTabOrder = Nothing  
     Set objPI = NothingEnd  
    Function 
    &#39;Do not break on an error. 
    On Error Resume Next 
    &#39;Call the addNewContacttoXML function, passing in the physical path to 
    &#39;save the file to and the name that you wish to use for the file. 
    addNewContacttoXML "c:","rolodex.xml" 
    &#39;Test to see if an error occurred, if so, let the user know. 
    &#39;Otherwise, tell the user that the operation was successful. 
    If err.number <> 0 then  
     Response.write("Errors occurred while saving your form submission.") 
    Else  
     Response.write("Your form submission has been saved.") 
    End If 
   %>


「HTML フォーム データを XML 形式で保存する」に関する記事を読んだことがある方は、HTML データを XML ファイルに拡張するために添付されたコードが HTML データ拡張子と同じであることに気づくでしょう。新しいドキュメントのコードは基本的に同じです。ただし、ここには 2 つの大きな違いがあります:

 &#39;Call the Load Method of the XMLDOM Object. The Load Method has a  
   &#39;boolean return value indicating whether or not the file could be  
   &#39;loaded. If the file exists and loads it will return true, otherwise,  
   &#39;it will return false.  
   blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)  
    
   &#39;Test to see if the file loaded successfully.  
   If blnFileExists = True Then  
    &#39;If the file loaded set the objRoot Object equal to the root element  
    &#39;of the XML document.  
    Set objRoot = objDom.documentElement 
   Else  
    &#39;Create your root element and append it to the XML document.  
    Set objRoot = objDom.createElement("contact")  
    objDom.appendChild objRoot  
   End If

このセクションのコードは addNewContacttoXML 関数から来ています。毎回新しいファイルを作成することはできないため、代わりに CONTACT を保存します。ファイルをロードできる場合は、XML ドキュメントのルート要素を取得します。ロードできない場合は、その要素が存在しないものとして新しい要素を作成し、それを XML ドキュメントに追加します。

もう 1 つの主な違いは、ファイルに対して二次チェックを実行して LOAD が成功したかどうかを確認するときに、処理命令を追加する必要があるかどうかを決定できることです。ファイルが存在する場合、このディレクティブを追加する必要はありません。ただし、新規ファイルを作成する場合には、この処理命令を追加する必要があります。


&#39;Check once again to see if the file loaded successfully. If it did  
  &#39;not, that means we are creating a new document and need to be sure to  
  &#39;insert the XML processing instruction.  
  If blnFileExists = False then  
   &#39;Create the xml processing instruction.  
   Set objPI = objDom.createProcessingInstruction("xml", "version=&#39;1.0&#39;")  
   &#39;Append the processing instruction to the XML document.  
   objDom.insertBefore objPI, objDom.childNodes(0)  
  End If

上記 2 つの違いを除けば、新しいファイルにデータを保存するコードは、既存のファイルに新しいレコードを追加するコードと実際には同じであることがわかります。新しく追加された各 RECORD に対応するために、新しい要素 contact CONTAINER を作成します。コードはリクエスト オブジェクトのフォーム コレクション内で反復されて、適切な XML ノードを作成し、ノード値が現在のフォーム フィールドと同じになるように設定されます。

上記は、XML ドキュメントに新しい「レコード」を追加する内容です。さらに関連する内容については、PHP 中国語 Web サイト (www.php.cn) に注目してください。


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
RSSの理解:XMLの視点RSSの理解:XMLの視点Apr 25, 2025 am 12:14 AM

RSSは、頻繁に更新されるコンテンツを公開するために使用されるXMLベースの形式です。 1。RSSFeedは、タイトル、リンク、説明などを含むXML構造を通じて情報を整理します。2。rssfeedの作成には、言語やリリース日などのメタデータを追加するXML構造での書き込みが必要です。 3.高度な使用法には、マルチメディアファイルと分類された情報を含めることができます。 4.デバッグ中にXML検証ツールを使用して、必要な要素が存在し、正しくエンコードされていることを確認します。 5. RSSFeedの最適化は、構造をシンプルに保つことで、ページング、キャッシュ、および保持することで実現できます。この知識を理解して適用することにより、コンテンツを効果的に管理および配布できます。

XMLのRSS:タグ、属性、および構造のデコードXMLのRSS:タグ、属性、および構造のデコードApr 24, 2025 am 12:09 AM

RSSは、コンテンツを公開および購読するために使用されるXMLベースの形式です。 RSSファイルのXML構造には、ルート要素、要素、および複数の要素が含まれ、それぞれがコンテンツエントリを表します。 XMLパーサーを介してRSSファイルを読み取り、解析すると、ユーザーは最新のコンテンツを購読して取得できます。

RSSにおけるXMLの利点:技術的なディープダイビングRSSにおけるXMLの利点:技術的なディープダイビングApr 23, 2025 am 12:02 AM

XMLには、RSSの構造化データ、スケーラビリティ、クロスプラットフォームの互換性、解析検証の利点があります。 1)構造化されたデータにより、コンテンツの一貫性と信頼性が保証されます。 2)スケーラビリティにより、コンテンツのニーズに合わせてカスタムタグを追加できます。 3)クロスプラットフォームの互換性により、さまざまなデバイスでシームレスに動作します。 4)分析および検証ツールは、フィードの品質と完全性を確保します。

XMLのRSS:コンテンツシンジケーションのコアを発表しますXMLのRSS:コンテンツシンジケーションのコアを発表しますApr 22, 2025 am 12:08 AM

XMLでのRSSの実装は、構造化されたXML形式を使用してコンテンツを整理することです。 1)RSSは、チャネル情報やプロジェクトリストなどの要素を含むデータ交換形式としてXMLを使用します。 2)RSSファイルを生成する場合、コンテンツは仕様に従って整理し、サブスクリプションのためにサーバーに公開する必要があります。 3)RSSファイルは、読者またはプラグインを介してサブスクライブして、コンテンツを自動的に更新できます。

基本を超えて:高度なRSSドキュメント機能基本を超えて:高度なRSSドキュメント機能Apr 21, 2025 am 12:03 AM

RSSの高度な機能には、コンテンツネームスペース、拡張モジュール、条件付きサブスクリプションが含まれます。 1)コンテンツネームスペースはRSS機能を拡張します。2)dublincoreやiTunesなどの拡張モジュールを拡張してメタデータを追加します。これらの関数は、情報収集の効率を改善するためにXML要素と属性を追加することにより実装されます。

XMLバックボーン:RSSフィードが構造化される方法XMLバックボーン:RSSフィードが構造化される方法Apr 20, 2025 am 12:02 AM

rssfeedsusexmltoStructurecontentupdates.1)xmlprovidesahierararchStructurefordata.2)theelementDefinesthefeed'sidentityandContainesements.3)letentionEntentividualContentPieces.4)

RSS&XML:Webコンテンツのダイナミックデュオを理解するRSS&XML:Webコンテンツのダイナミックデュオを理解するApr 19, 2025 am 12:03 AM

RSSとXMLは、Webコンテンツ管理のためのツールです。 RSSはコンテンツの公開と購読に使用され、XMLはデータの保存と転送に使用されます。コンテンツの公開、サブスクリプション、および更新プッシュで動作します。使用法の例には、RSS公開ブログ投稿やXML保存本情報が含まれます。

RSSドキュメント:Webシンジケーションの基礎RSSドキュメント:Webシンジケーションの基礎Apr 18, 2025 am 12:04 AM

RSSドキュメントは、頻繁に更新されるコンテンツを公開および購読するために使用されるXMLベースの構造化されたファイルです。その主な機能には、1)自動化されたコンテンツの更新、2)コンテンツの集約、3)ブラウジング効率の改善。 RSSFeedを通じて、ユーザーはタイムリーにさまざまなソースから最新情報を購読および取得できます。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター