私は表形式の出力を得るためのこのJavaScript htmlコードを持っています。 XMLデータをスクリプトに入力しています。javascriptの表形式の出力
function myFunction() {
parser=new DOMParser();
xmlDoc=parser.parseFromString(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+"<book>"+
"<title>The Time Machine and Other Stories</title>"+
"<author_name>H. G. Wells</author_name>"+
"<publish_date>2016</publish_date>"+
"<cost currency=\"USD\">10</cost>"+
"<publisher_info>"+
"<publisher_name>Read Books Ltd</publisher_name>"+
"<publisher_address>"+
"<street_name>Evesham street</street_name>"+
"<city>Worcestershire</city>"+
"<zip_code>11WR</zip_code>"+
"<country>United Kingdom</country>"+
"</publisher_address>"+
"</publisher_info>"+
"</book>","text/xml");
var x=xmlDoc.getElementsByTagName("publisher_address");
for (i=0;i<x.length;i++) {
document.write("<tr>");
var y=x[i].childNodes;
for (j=0;j<y.length;j++) {
document.write("<td>"+ y[j].childNodes[0].nodeValue+ "</td>");
}
document.write("</tr>");
}
}
table, th, td {
border: 1px solid black;
}
<h1>My First XML Parsing JavaScript</h1>
<p>Click the button to display book info.</p>
<p id="demo"></p>
<button type="button" onclick="myFunction()">Try it</button>
<table border="0">
</table>
私は取得しています出力は次のようになります。
イヴシャムstreetWorcestershire11WRUnited王国
私は表形式で出力を取得することはできませんよ、なぜ誰も私を助けることができます?
これは、XMLページに許可されていません[ 'document.write'](https://developer.mozilla.org/en-US/docs/Web/API/Document/write)のためです。 – Teemu
どのように結果を表形式で表示できますか? – Pramod
'createElement'や' appendChild'のような適切なDOM操作メソッドを使ってテーブルを作成します。 – Teemu