2011-02-26 6 views
2

javascript変数にいくつかのHTML/XMLマークアップを保存したいと思います。問題は、テキストが比較的大きいことです。たとえば、次のXMLピースをjavascript変数に格納するにはどうすればよいですか?javascript変数にHTMLまたはXMLコードを格納する

<QuestionForm xmlns="[the QuestionForm schema URL]"> 
    <Overview> 
    <Title>Game 01523, "X" to play</Title> 
    <Text> 
     You are helping to decide the next move in a game of Tic-Tac-Toe. The board looks like this: 
    </Text> 
    <Binary> 
     <MimeType> 
     <Type>image</Type> 
     <SubType>gif</SubType> 
     </MimeType> 
     <DataURL>http://tictactoe.amazon.com/game/01523/board.gif</DataURL> 
     <AltText>The game board, with "X" to move.</AltText> 
    </Binary> 
    <Text> 
     Player "X" has the next move. 
    </Text> 
    </Overview> 
    <Question> 
    <QuestionIdentifier>nextmove</QuestionIdentifier> 
    <DisplayName>The Next Move</DisplayName> 
    <IsRequired>true</IsRequired> 
    <QuestionContent> 
     <Text> 
     What are the coordinates of the best move for player "X" in this game? 
     </Text> 
    </QuestionContent> 
    <AnswerSpecification> 
     <FreeTextAnswer> 
     <Constraints> 
      <Length minLength="2" maxLength="2" /> 
     </Constraints> 
     <DefaultText>C1</DefaultText> 
     </FreeTextAnswer> 
    </AnswerSpecification> 
    </Question> 
    <Question> 
    <QuestionIdentifier>likelytowin</QuestionIdentifier> 
    <DisplayName>The Next Move</DisplayName> 
    <IsRequired>true</IsRequired> 
    <QuestionContent> 
     <Text> 
     How likely is it that player "X" will win this game? 
     </Text> 
    </QuestionContent> 
    <AnswerSpecification> 
     <SelectionAnswer> 
     <StyleSuggestion>radiobutton</StyleSuggestion> 
     <Selections> 
      <Selection> 
      <SelectionIdentifier>notlikely</SelectionIdentifier> 
      <Text>Not likely</Text> 
      </Selection> 
      <Selection> 
      <SelectionIdentifier>unsure</SelectionIdentifier> 
      <Text>It could go either way</Text> 
      </Selection> 
      <Selection> 
      <SelectionIdentifier>likely</SelectionIdentifier> 
      <Text>Likely</Text> 
      </Selection> 
     </Selections> 
     </SelectionAnswer> 
    </AnswerSpecification> 
    </Question> 
</QuestionForm> 
+0

難易度はどうですか?書式について(たとえば文字列として、またはDOMオブジェクトとして)疑問に思っていますか?他に何か? – outis

+0

これを文字列として保存する最も簡単な方法を探したいと思います。 – Mark

答えて

-9
var string = (<r><![CDATA[ 

    The text string goes here. Since this is a XML CDATA section, 
    stuff like <> work fine too, even if definitely invalid XML. 

]]></r>).toString(); 
+0

ありがとうございますが、これはChromeで動作しません、私に "予期しないトークン"を与えます – Mark

+12

これはどんな種類のクレイジームーン言語です! JavaScriptはありません。 – Domenic

+0

ありがとうございました – Mallik

0

私はJSON、mateyを使用することをお勧めします。あまり冗長ではありません。そしてjavascriptにはそれをうまく処理するメソッドがあります。クラスの後半でXMLを操作する必要がある場合は、簡単なアダプタを作成できます。

8

ウェブサービスやAjaxコールなどから取得したものではなく、正確な文字列をどうやって取得するのかという疑問があります。この質問に答えるために、理由の多くのためにかなり悪い考えですが... JavaScriptでこれを行うことのない、本当に良い方法は


はありません。一部のブラウザでは、行の末尾に\を配置することによって、行の継続をサポートするので、あなたのようなものだろう:

var xml = '<QuestionForm xmlns="[the QuestionForm schema URL]">\ 
    <Overview>\ 
    <Title>Game 01523, "X" to play</Title>\ 
    ...'; 

をしかし、これは非標準であり、実際には直接JS仕様と矛盾:

の前にバックスラッシュがある場合でも、 'LineTerminator'文字は を文字列リテラルに含めることはできません。これを行うための

ザ・だけは本当に信頼できる方法は手動の文字列連結で次のようになります。

var xml = '<QuestionForm xmlns="[the QuestionForm schema URL]">' + "\n" + 
'  <Overview>' + "\n" + 
'  <Title>Game 01523, "X" to play</Title>' + "\n" + 
'  ...'; 

あなたは少しすっきりそうのように、このことができます:

var xml = ['<QuestionForm xmlns="[the QuestionForm schema URL]">', 
'  <Overview>', 
'  <Title>Game 01523, "X" to play</Title>' 
'  ...'].join("\n"); 

しかし、それはまだ吸います。

また、すべてのアプローチでは、一重引用符をエスケープする必要があります。つまり、'\'に置き換えます。私はあなたのXMLスニペットで、一見したところで何も見ませんでしたが、これは良いことです。

+0

HTMLを変数に含めることができる他のクロスブラウザーの方法をご存知ですか? – Pentium10

+0

@ Pentium10 no ... – Domenic

+0

私はそれを正しくしています。 JavaScript文字列に '\ n'を使用することはできませんか? – omikron

4

私はあなたがjavascriptでhtml/xmlコードを保存し、textareaまたはその他の入力要素で表示したいと思っていたと思います。これがあなたの意図なら、これはあなたを助けるでしょう。

javascript変数の代わりに、divにコードを格納し、css ex:display:noneを使用して非表示にします。コードを表示する場合は、$('#div id').text()を使用できます。

関連する問題