[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/API/DOMParser/parseFromString [Back]  [Original]

DOMParser: parseFromString() - Web API | MDN

MDN Web Docs

View in English Always switch to English

DOMParser: parseFromString()

Baseline

20157

parseFromString() DOMParser HTML XML HTMLDocument XMLDocument

js
parseFromString(string, mimeType)

string

HTMLxmlXHTMLsvg

mimeType

XML HTML

  • text/html
  • text/xml
  • application/xml
  • application/xhtml+xml
  • image/svg+xml

text/html HTML HTMLDocument <script> <noscript>

(text/xml, application/xml, application/xhtml+xml, image/svg+xml) XML XMLDocument

TypeError

HTMLDocument XMLDocument mimeType

XMLSVGHTML

MIME text/html HTML MIME XML MIME application/xml image/svg+xml SVG 2

js
const parser = new DOMParser();

const xmlString = "<warning>Beware of the tiger</warning>";
const doc1 = parser.parseFromString(xmlString, "application/xml");
// XMLDocument

const svgString = '<circle cx="50" cy="50" r="50"/>';
const doc2 = parser.parseFromString(svgString, "image/svg+xml");
// XMLDocument

const htmlString = "<strong>Beware of the leopard</strong>";
const doc3 = parser.parseFromString(htmlString, "text/html");
// HTMLDocument

console.log(doc1.documentElement.textContent);
// "Beware of the tiger"

console.log(doc2.firstChild.tagName);
// "circle"

console.log(doc3.body.firstChild.textContent);
// "Beware of the leopard"

XML XML parseFromString XMLDocument <parsererror>

js
const parser = new DOMParser();

const xmlString = "<warning>Beware of the missing closing tag";
const doc = parser.parseFromString(xmlString, "application/xml");
const errorNode = doc.querySelector("parsererror");
if (errorNode) {
  // 
} else {
  // 
}

JavaScript

HTML
# dom-domparser-parsefromstring-dev


Web Proxy Viewer  |  New URL  |  Original Page