{"id":3764,"date":"2023-12-26T21:40:55","date_gmt":"2023-12-26T13:40:55","guid":{"rendered":"http:\/\/www.ponybai.com\/?p=3764"},"modified":"2023-11-27T21:46:15","modified_gmt":"2023-11-27T13:46:15","slug":"xml-%e6%94%af%e6%8c%81","status":"publish","type":"post","link":"http:\/\/www.ponybai.com\/?p=3764","title":{"rendered":"XML \u652f\u6301"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex \u63d0\u4f9b\u4e86\u5b9e\u7528\u7a0b\u5e8f\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790 \u4f7f\u7528\u6d41\u548c DOM \u7684 XML \u5185\u5bb9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u8282\u5305\u542b\u6709\u5173 XML \u652f\u6301\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 Streams<br>\u8bfb\u53d6\u548c\u5199\u5165 XML Apex \u63d0\u4f9b\u4e86\u7528\u4e8e<strong>\u4f7f\u7528\u6d41\u8bfb\u53d6\u548c\u5199\u5165 XML<\/strong>\u00a0\u5185\u5bb9\u7684\u7c7b\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u00a0<strong>DOM<\/strong><br>\u8bfb\u53d6\u548c\u5199\u5165 XML Apex \u63d0\u4f9b\u4e86\u4f7f\u60a8\u80fd\u591f\u4f7f\u7528 DOM\uff08\u6587\u6863\u5bf9\u8c61\u6a21\u578b\uff09\u5904\u7406 XML \u5185\u5bb9\u7684\u7c7b\u3002<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">\u4f7f\u7528\u6d41\u8bfb\u53d6\u548c\u5199\u5165 XML<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Apex \u63d0\u4f9b\u4e86\u7528\u4e8e\u4f7f\u7528\u6d41\u8bfb\u53d6\u548c\u5199\u5165 XML \u5185\u5bb9\u7684\u7c7b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 XMLStreamReader \u7c7b\u53ef\u4ee5\u8bfb\u53d6 XML \u5185\u5bb9\u548c XMLStreamWriter \u7c7b\u4f7f\u60a8\u80fd\u591f\u7f16\u5199 XML \u5185\u5bb9\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u6d41<\/strong><br>\u8bfb\u53d6 XML XMLStreamReader \u7c7b\u65b9\u6cd5\u652f\u6301\u5bf9 XML \u6570\u636e\u7684\u6b63\u5411\u53ea\u8bfb\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u6d41<\/strong><br>\u7f16\u5199 XML XmlStreamWriter \u7c7b\u65b9\u6cd5\u5141\u8bb8\u5199\u5165 XML \u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">\u4f7f\u7528\u6d41\u8bfb\u53d6 XML<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">XMLStreamReader \u7c7b\u65b9\u6cd5\u652f\u6301\u5bf9 XML \u7684\u6b63\u5411\u53ea\u8bfb\u8bbf\u95ee \u6570\u636e\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0e HTTP \u6807\u6ce8\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u89e3\u6790 XML \u6570\u636e\u6216\u8df3\u8fc7 \u4e0d\u9700\u8981\u7684\u4e8b\u4ef6\u3002\u60a8\u53ef\u4ee5\u5206\u6790\u6700<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a><\/a>50\u8282\u70b9\u6df1\u5ea6\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u6f14\u793a\u5982\u4f55 \u5b9e\u4f8b\u5316\u65b0\u7684 XmlStreamReader \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String xmlString = '&lt;books&gt;&lt;book&gt;My Book&lt;\/book&gt;&lt;book&gt;Your Book&lt;\/book&gt;&lt;\/books&gt;';\nXmlStreamReader xsr = new XmlStreamReader(xmlString);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u65b9\u6cd5\u9002\u7528\u4e8e\u4ee5\u4e0b XML \u4e8b\u4ef6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e3a\u7279\u5b9a\u5143\u7d20\u6307\u5b9a<dfn>\u5c5e\u6027<\/dfn>\u4e8b\u4ef6\u3002\u4f8b\u5982 \u8be5\u5143\u7d20\u5177\u6709\u5c5e\u6027\uff1a\u3002<samp>&lt;book&gt;<\/samp><samp>title<\/samp><samp>&lt;book&nbsp;title=&#8221;Salesforce.com for Dummies&#8221;&gt;<\/samp><\/li>\n\n\n\n<li>start \u5143\u7d20\u4e8b\u4ef6\u662f<dfn>\u5143\u7d20<\/dfn>\u7684\u5f00\u59cb\u6807\u8bb0\uff0c\u4f8b\u5982 \u3002<samp>&lt;book&gt;<\/samp><\/li>\n\n\n\n<li>\u7ed3\u675f\u5143\u7d20\u4e8b\u4ef6\u662f<dfn>\u5143\u7d20\u7684\u7ed3\u675f<\/dfn>\u6807\u8bb0\uff0c\u4f8b\u5982 \u3002<samp>&lt;\/book&gt;<\/samp><\/li>\n\n\n\n<li>\u5f00\u59cb\u6587\u6863\u4e8b\u4ef6\u662f<dfn>\u6587\u6863\u7684\u5f00\u59cb<\/dfn>\u6807\u8bb0\u3002<\/li>\n\n\n\n<li>\u7ed3\u675f\u6587\u6863\u4e8b\u4ef6\u662f<dfn>\u6587\u6863\u7684\u7ed3\u675f<\/dfn>\u6807\u8bb0\u3002<\/li>\n\n\n\n<li>\u5b9e\u4f53\u5f15\u7528\u662f\u4ee3\u7801\u4e2d\u7684<dfn>\u5b9e\u4f53\u5f15\u7528<\/dfn>\uff0c\u4f8b\u5982 \u3002<samp>!ENTITY title = &#8220;My Book Title&#8221;<\/samp><\/li>\n\n\n\n<li>\u5b57\u7b26\u4e8b\u4ef6\u662f\u6587\u672c<dfn>\u5b57\u7b26<\/dfn>\u3002<\/li>\n\n\n\n<li><dfn>\u6ce8\u91ca<\/dfn>\u4e8b\u4ef6\u662f XML \u6587\u4ef6\u4e2d\u7684\u6ce8\u91ca\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 and \u65b9\u6cd5\u5faa\u73af\u8bbf\u95ee XML \u6570\u636e\u3002\u4f7f\u7528 \u65b9\u6cd5\uff08\u5982 the \u65b9\u6cd5\uff09\u8bbf\u95ee XML \u4e2d\u7684\u6570\u636e\u3002<samp>next<\/samp><samp>hasNext<\/samp><samp>get<\/samp><samp>getNamespace<\/samp><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5faa\u73af\u8bbf\u95ee XML \u6570\u636e\u65f6\uff0c\u8bf7\u59cb\u7ec8\u5728\u8c03\u7528\u4e4b\u524d\u68c0\u67e5\u6d41\u6570\u636e\u662f\u5426\u53ef\u7528\uff0c\u4ee5\u907f\u514d\u5c1d\u8bd5\u8bfb\u53d6 XML \u6570\u636e\u3002<samp>hasNext<\/samp><samp>next<\/samp><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>XmlStreamReader \u793a\u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u793a\u4f8b\u5904\u7406\u4e00\u4e2a XML \u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class XmlStreamReaderDemo {\n\n    \/\/ Create a class Book for processing\n    public class Book {\n        String name;\n        String author;\n    }\n\n    public Book&#91;] parseBooks(XmlStreamReader reader) {\n        Book&#91;] books = new Book&#91;0];\n        boolean isSafeToGetNextXmlElement = true;\n        while(isSafeToGetNextXmlElement) {\n            \/\/ Start at the beginning of the book and make sure that it is a book\n            if (reader.getEventType() == XmlTag.START_ELEMENT) {\n                if ('Book' == reader.getLocalName()) {\n                    \/\/ Pass the book to the parseBook method (below) \n                    Book book = parseBook(reader);\n                    books.add(book);\n                }\n            }\n            \/\/ Always use hasNext() before calling next() to confirm \n            \/\/ that we have not reached the end of the stream\n            if (reader.hasNext()) {\n                reader.next();\n            } else {\n                isSafeToGetNextXmlElement = false;\n                break;\n            }\n        }\n        return books;\n    }\n\n    \/\/ Parse through the XML, determine the author and the characters\n    Book parseBook(XmlStreamReader reader) {\n        Book book = new Book();\n        book.author = reader.getAttributeValue(null, 'author');\n        boolean isSafeToGetNextXmlElement = true;\n        while(isSafeToGetNextXmlElement) {\n            if (reader.getEventType() == XmlTag.END_ELEMENT) {\n                break;\n            } else if (reader.getEventType() == XmlTag.CHARACTERS) {\n                book.name = reader.getText();\n            }\n            \/\/ Always use hasNext() before calling next() to confirm \n            \/\/ that we have not reached the end of the stream\n            if (reader.hasNext()) {\n                reader.next();\n            } else {\n                isSafeToGetNextXmlElement = false;\n                break;\n            }\n        }\n        return book;\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@isTest\nprivate class XmlStreamReaderDemoTest {\n    \/\/ Test that the XML string contains specific values\n    static testMethod void testBookParser() {\n\n        XmlStreamReaderDemo demo = new XmlStreamReaderDemo();\n\n        String str = '&lt;books&gt;&lt;book author=\"Chatty\"&gt;Alpha beta&lt;\/book&gt;' +\n            '&lt;book author=\"Sassy\"&gt;Baz&lt;\/book&gt;&lt;\/books&gt;';\n\n        XmlStreamReader reader = new XmlStreamReader(str);\n        XmlStreamReaderDemo.Book&#91;] books = demo.parseBooks(reader);\n\n        System.debug(books.size());\n\n        for (XmlStreamReaderDemo.Book book : books) {\n            System.debug(book);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u4f7f\u7528\u6d41\u7f16\u5199 XML<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">XmlStreamWriter \u7c7b\u65b9\u6cd5\u5141\u8bb8\u7f16\u5199 XML \u6570\u636e\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0e HTTP \u6807\u6ce8\u7ed3\u5408\u4f7f\u7528\u4ee5\u6784\u9020 \u8981\u5728\u6807\u6ce8\u8bf7\u6c42\u4e2d\u53d1\u9001\u5230\u5916\u90e8\u670d\u52a1\u7684 XML \u6587\u6863\u3002 \u4e0b\u9762\u7684\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u5b9e\u4f8b\u5316\u65b0\u7684 XmlStreamReader \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String xmlString = '&lt;books&gt;&lt;book&gt;My Book&lt;\/book&gt;&lt;book&gt;Your Book&lt;\/book&gt;&lt;\/books&gt;';\nXmlStreamReader xsr = new XmlStreamReader(xmlString);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>XML \u7f16\u5199\u5668\u65b9\u6cd5 \u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u793a\u4f8b\u7f16\u5199\u4e00\u4e2a XML \u6587\u6863 \u5e76\u6d4b\u8bd5\u5176\u6709\u6548\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64 Hello World \u793a\u4f8b\u9700\u8981\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\u60a8\u53ef\u4ee5 \u60a8\u53ef\u4ee5\u81ea\u884c\u521b\u5efa\u8fd9\u4e9b\u4ee3\u7801\uff0c\u6216\u5c06\u5bf9\u8c61\u548c Apex \u4ee3\u7801\u4e0b\u8f7d\u4e3a\u975e\u6258\u7ba1\u5305 \u6765\u81ea Salesforce AppExchange\u3002\u8981\u83b7\u53d6\u7ec4\u7ec7\u4e2d\u7684\u793a\u4f8b\u8d44\u4ea7\uff0c\u8bf7\u5b89\u88c5\u00a0Apex \u6559\u7a0b\u5305\u3002\u6b64\u5957\u9910 \u8fd8\u5305\u542b\u88c5\u8fd0\u53d1\u7968\u793a\u4f8b\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class XmlWriterDemo {\n\n     public String getXml() {\n          XmlStreamWriter w = new XmlStreamWriter();\n          w.writeStartDocument(null, '1.0');\n          w.writeProcessingInstruction('target', 'data');\n          w.writeStartElement('m', 'Library', 'http:\/\/www.book.com');\n          w.writeNamespace('m', 'http:\/\/www.book.com');\n          w.writeComment('Book starts here');\n          w.setDefaultNamespace('http:\/\/www.defns.com');\n          w.writeCData('&lt;Cdata&gt; I like CData &lt;\/Cdata&gt;');\n          w.writeStartElement(null, 'book', null);\n          w.writedefaultNamespace('http:\/\/www.defns.com');\n          w.writeAttribute(null, null, 'author', 'Manoj');\n          w.writeCharacters('This is my book');\n          w.writeEndElement(); \/\/end book\n          w.writeEmptyElement(null, 'ISBN', null);\n          w.writeEndElement(); \/\/end library\n          w.writeEndDocument();\n          String xmlOutput = w.getXmlString();\n          w.close();\n          return xmlOutput;\n        }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@isTest\nprivate class XmlWriterDemoTest {\n    static TestMethod void basicTest() {\n        XmlWriterDemo demo = new XmlWriterDemo();\n        String result = demo.getXml();\n        String expected = '&lt;?xml version=\"1.0\"?&gt;&lt;?target data?&gt;' +\n            '&lt;m:Library xmlns:m=\"http:\/\/www.book.com\"&gt;' + \n            '&lt;!--Book starts here--&gt;' +\n            '&lt;!&#91;CDATA&#91;&lt;Cdata&gt; I like CData &lt;\/Cdata&gt;]]&gt;' +\n'&lt;book xmlns=\"http:\/\/www.defns.com\" author=\"Manoj\"&gt;This is my book&lt;\/book&gt;&lt;ISBN\/&gt;&lt;\/m:Library&gt;';\n        \n        System.assert(result == expected);\n    }\n}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u4f7f\u7528 DOM \u8bfb\u53d6\u548c\u5199\u5165 XML<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Apex \u63d0\u4f9b\u4e86\u4f7f\u60a8\u80fd\u591f\u4f7f\u7528 DOM \u5904\u7406 XML \u5185\u5bb9\u7684\u7c7b \uff08\u6587\u6863\u5bf9\u8c61\u6a21\u578b\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">DOM \u7c7b\u53ef\u5e2e\u52a9\u60a8\u89e3\u6790\u6216\u751f\u6210 XML \u5185\u5bb9\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u6765\u5de5\u4f5c \u66ff\u6362\u4e3a\u4efb\u4f55 XML \u5185\u5bb9\u3002\u4e00\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u662f\u4f7f\u7528\u7c7b\u6765\u751f\u6210\u6b63\u6587 \u7531\u00a0HttpRequest\u00a0\u521b\u5efa\u7684\u8bf7\u6c42\u6216\u89e3\u6790\u00a0HttpResponse\u00a0\u8bbf\u95ee\u7684\u54cd\u5e94\u3002The DOM \u5c06 XML \u6587\u6863\u8868\u793a\u4e3a\u8282\u70b9\u5c42\u6b21\u7ed3\u6784\u3002\u67d0\u4e9b\u8282\u70b9\u53ef\u80fd\u662f\u5206\u652f\u8282\u70b9 \u5e76\u4e14\u6709\u5b50\u8282\u70b9\uff0c\u800c\u5176\u4ed6\u8282\u70b9\u662f\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u53f6\u8282\u70b9\u3002\u60a8\u53ef\u4ee5 \u5206\u6790\u5d4c\u5957\u7684 XML \u5185\u5bb9\uff0c\u8be5\u5185\u5bb9\u6700\u591a50\u8282\u70b9 \u6df1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">DOM \u7c7b\u5305\u542b\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u3002<samp>Dom<\/samp><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u00a0Document \u7c7b\u5904\u7406 XML \u6587\u6863\u6b63\u6587\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u00a0XmlNode \u7c7b\u5904\u7406 XML \u4e2d\u7684\u8282\u70b9 \u516c\u6587\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 Document \u7c7b\u7c7b\u5904\u7406 XML \u5185\u5bb9\u3002\u4e00\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u662f\u4f7f\u7528\u5b83 \u4e3a\u00a0HttpRequest\u00a0\u521b\u5efa\u8bf7\u6c42\u7684\u6b63\u6587\uff0c\u6216\u5206\u6790\u00a0HttpResponse\u00a0\u8bbf\u95ee\u7684\u54cd\u5e94\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>XML \u547d\u540d\u7a7a\u95f4<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">XML \u547d\u540d\u7a7a\u95f4\u662f\u7531 URI \u5f15\u7528\u6807\u8bc6\u7684\u540d\u79f0\u96c6\u5408\uff0c\u7528\u4e8e XML \u6587\u6863\uff0c\u7528\u4e8e\u552f\u4e00\u6807\u8bc6\u5143\u7d20\u7c7b\u578b\u548c\u5c5e\u6027\u540d\u79f0\u3002XML \u4e2d\u7684\u540d\u79f0 \u547d\u540d\u7a7a\u95f4\u53ef\u80fd\u663e\u793a\u4e3a\u9650\u5b9a\u540d\u79f0\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u5192\u53f7\uff0c\u5206\u9694 \u540d\u79f0\u8f6c\u6362\u4e3a\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u548c\u672c\u5730\u90e8\u5206\u3002\u524d\u7f00\uff0c\u6620\u5c04\u5230 URI \u5f15\u7528\uff0c\u9009\u62e9\u547d\u540d\u7a7a\u95f4\u3002\u901a\u7528\u7ba1\u7406 URI \u7684\u7ec4\u5408 \u547d\u540d\u7a7a\u95f4\u548c\u6587\u6863\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\u751f\u6210\u901a\u7528\u7684\u6807\u8bc6\u7b26 \u72ec\u7279\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b XML \u5143\u7d20\u7684\u547d\u540d\u7a7a\u95f4\u548c\u524d\u7f00\u4e3a \u3002<samp>http:\/\/my.name.space<\/samp><samp>myprefix<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;sampleElement xmlns:myprefix=\"http:\/\/my.name.space\" \/&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0cXML \u5143\u7d20\u5177\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u4e2a\u5c5e\u6027\u7684\u952e\u4e3a ; \u503c\u4e3a \u3002<samp>dimension<\/samp><samp>2<\/samp><\/li>\n\n\n\n<li>\u7b2c\u4e8c\u4e2a\u5c5e\u6027\u7684\u952e\u547d\u540d\u7a7a\u95f4\u4e3a&nbsp;http:\/\/ns1;\u8fd9 value \u547d\u540d\u7a7a\u95f4\u4e3a&nbsp;http:\/\/ns2;\u5173\u952e\u662f;\u503c\u4e3a \u3002<samp>example<\/samp><samp>test<\/samp><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;square dimension=\"2\" ns1:example=\"ns2:test\" xmlns:ns1=\"http:\/\/ns1\" xmlns:ns2=\"http:\/\/ns2\" \/&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516c\u6587\u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u4e0b\u9762\u7684\u793a\u4f8b\uff0c\u5047\u5b9a\u4f20\u9012\u5230\u65b9\u6cd5\u4e2d\u7684\u53c2\u6570\u8fd4\u56de\u6b64 XML \u54cd\u5e94\uff1a<samp>url<\/samp><samp>parseResponseDom<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;address&gt;\n    &lt;name&gt;Kirk Stevens&lt;\/name&gt;\n    &lt;street1&gt;808 State St&lt;\/street1&gt;\n    &lt;street2&gt;Apt. 2&lt;\/street2&gt;\n    &lt;city&gt;Palookaville&lt;\/city&gt;\n    &lt;state&gt;PA&lt;\/state&gt;\n    &lt;country&gt;USA&lt;\/country&gt;\n&lt;\/address&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 DOM \u7c7b\u6765\u5206\u6790 XML \u54cd\u5e94 \u5728\u8bf7\u6c42\u6b63\u6587\u4e2d\u8fd4\u56de\uff1a<samp>GET<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DomDocument {\n \n    \/\/ Pass in the URL for the request\n    \/\/ For the purposes of this sample,assume that the URL\n    \/\/ returns the XML shown above in the response body\n    public void parseResponseDom(String url){\n        Http h = new Http();\n        HttpRequest req = new HttpRequest();\n        \/\/ url that returns the XML in the response body\n        req.setEndpoint(url);\n        req.setMethod('GET');\n        HttpResponse res = h.send(req);\n        Dom.Document doc = res.getBodyDocument();\n        \n        \/\/Retrieve the root element for this document.\n        Dom.XMLNode address = doc.getRootElement();\n        \n        String name = address.getChildElement('name', null).getText();\n        String state = address.getChildElement('state', null).getText();\n        \/\/ print out specific elements\n        System.debug('Name: ' + name);\n        System.debug('State: ' + state);\n        \n        \/\/ Alternatively, loop through the child elements.\n        \/\/ This prints out all the elements of the address\n        for(Dom.XMLNode child : address.getChildElements()) {\n           System.debug(child.getText());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528 XML \u8282\u70b9<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u8be5\u7c7b\u5904\u7406 XML \u6587\u6863\u3002DOM \u5c06 XML \u6587\u6863\u8868\u793a\u4e3a\u8282\u70b9\u7684\u5c42\u6b21\u7ed3\u6784\u3002\u4e00\u4e9b \u8282\u70b9\u53ef\u4ee5\u662f\u5206\u652f\u8282\u70b9\u5e76\u5177\u6709\u5b50\u8282\u70b9\uff0c\u800c\u5176\u4ed6\u8282\u70b9\u662f\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u53f6\u8282\u70b9 \u5b69\u5b50\u3002<samp>XmlNode<\/samp><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Apex \u4e2d\u6709\u4e0d\u540c\u7c7b\u578b\u7684 DOM \u8282\u70b9\u53ef\u7528\u3002 \u662f\u8fd9\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u679a\u4e3e\u3002 \u8fd9\u4e9b\u503c\u4e3a\uff1a<samp>XmlNodeType<\/samp><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bc4\u8bba<\/li>\n\n\n\n<li>\u5143\u7d20<\/li>\n\n\n\n<li>\u53d1\u77ed\u4fe1<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u533a\u5206 XML \u6587\u6863\u4e2d\u7684\u5143\u7d20\u548c\u8282\u70b9\u975e\u5e38\u91cd\u8981\u3002\u8fd9 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684 XML \u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;name&gt;\n    &lt;firstName&gt;Suvain&lt;\/firstName&gt;\n    &lt;lastName&gt;Singh&lt;\/lastName&gt;\n&lt;\/name&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u793a\u4f8b\u5305\u542b\u4e09\u4e2a XML \u5143\u7d20\uff1a\u3001 \u548c \u3002\u5b83\u5305\u542b\u4e94\u4e2a\u8282\u70b9\uff1a\u4e09\u4e2a\u8282\u70b9 \u3001 \u548c \u5143\u7d20 \u8282\u70b9\uff0c\u4ee5\u53ca\u4e24\u4e2a\u6587\u672c\u8282\u70b9 &#8211; \u548c .\u8bf7\u6ce8\u610f\uff0c\u5143\u7d20\u4e2d\u7684\u6587\u672c \u8282\u70b9\u88ab\u89c6\u4e3a\u5355\u72ec\u7684\u6587\u672c\u8282\u70b9\u3002<samp>name<\/samp><samp>firstName<\/samp><samp>lastName<\/samp><samp>name<\/samp><samp>firstName<\/samp><samp>lastName<\/samp><samp>Suvain<\/samp><samp>Singh<\/samp><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u5173\u6240\u6709\u679a\u4e3e\u5171\u4eab\u7684\u65b9\u6cd5\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u679a\u4e3e\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>XmlNode\u7684\u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u65b9\u6cd5 \u548c\u547d\u540d\u7a7a\u95f4\u6765\u521b\u5efa XML \u8bf7\u6c42\u3002<samp>XmlNode<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DomNamespaceSample\n{\n    public void sendRequest(String endpoint)\n    {\n        \/\/ Create the request envelope\n        DOM.Document doc = new DOM.Document();\n        \n        String soapNS = 'http:\/\/schemas.xmlsoap.org\/soap\/envelope\/';\n        String xsi = 'http:\/\/www.w3.org\/2001\/XMLSchema-instance';\n        String serviceNS = 'http:\/\/www.myservice.com\/services\/MyService\/';\n        \n        dom.XmlNode envelope\n            = doc.createRootElement('Envelope', soapNS, 'soapenv');\n        envelope.setNamespace('xsi', xsi);\n        envelope.setAttributeNS('schemaLocation', soapNS, xsi, null);\n        \n        dom.XmlNode body\n            = envelope.addChildElement('Body', soapNS, null);\n        \n        body.addChildElement('echo', serviceNS, 'req').\n           addChildElement('category', serviceNS, null).\n           addTextNode('classifieds');\n        \n        System.debug(doc.toXmlString());\n        \n        \/\/ Send the request\n        HttpRequest req = new HttpRequest();\n        req.setMethod('POST');\n        req.setEndpoint(endpoint);\n        req.setHeader('Content-Type', 'text\/xml');\n        \n        req.setBodyDocument(doc);\n        \n        Http http = new Http();\n        HttpResponse res = http.send(req);\n        \n        System.assertEquals(200, res.getStatusCode());\n        \n        dom.Document resDoc = res.getBodyDocument();\n        \n        envelope = resDoc.getRootElement();\n        \n        String wsa = 'http:\/\/schemas.xmlsoap.org\/ws\/2004\/08\/addressing';\n        \n        dom.XmlNode header = envelope.getChildElement('Header', soapNS);\n        System.assert(header != null);\n        \n        String messageId\n            = header.getChildElement('MessageID', wsa).getText();\n        \n        System.debug(messageId);\n        System.debug(resDoc.toXmlString());\n        System.debug(resDoc);\n        System.debug(header);\n        \n        System.assertEquals(\n         'http:\/\/schemas.xmlsoap.org\/ws\/2004\/08\/addressing\/role\/anonymous',\n         header.getChildElement(\n           'ReplyTo', wsa).getChildElement('Address', wsa).getText());\n        \n        \n        System.assertEquals(\n          envelope.getChildElement('Body', soapNS).\n              getChildElement('echo', serviceNS).\n              getChildElement('something', 'http:\/\/something.else').\n              getChildElement(\n                'whatever', serviceNS).getAttribute('bb', null),\n                'cc');\n        \n        System.assertEquals('classifieds',\n          envelope.getChildElement('Body', soapNS).\n              getChildElement('echo', serviceNS).\n              getChildElement('category', serviceNS).getText());\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Apex \u63d0\u4f9b\u4e86\u5b9e\u7528\u7a0b\u5e8f\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790 \u4f7f\u7528\u6d41\u548c DOM \u7684 XML \u5185\u5bb9\u3002<\/p>\n","protected":false},"author":1,"featured_media":3765,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[199],"tags":[226],"class_list":["post-3764","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apex-","tag-apex-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>XML \u652f\u6301 - \u767d\u767d\u968f\u7b14<\/title>\n<meta name=\"description\" content=\"Apex \u63d0\u4f9b\u4e86\u5b9e\u7528\u7a0b\u5e8f\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790 \u4f7f\u7528\u6d41\u548c DOM \u7684 XML \u5185\u5bb9\u3002\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.ponybai.com\/?p=3764\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"XML \u652f\u6301 - \u767d\u767d\u968f\u7b14\" \/>\n<meta property=\"og:description\" content=\"Apex \u63d0\u4f9b\u4e86\u5b9e\u7528\u7a0b\u5e8f\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790 \u4f7f\u7528\u6d41\u548c DOM \u7684 XML \u5185\u5bb9\u3002\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.ponybai.com\/?p=3764\" \/>\n<meta property=\"og:site_name\" content=\"\u767d\u767d\u968f\u7b14\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-26T13:40:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-27T13:46:15+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.ponybai.com\/wp-content\/uploads\/2023\/11\/xml.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1372\" \/>\n\t<meta property=\"og:image:height\" content=\"205\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"ponybai\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"ponybai\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/www.ponybai.com\/?p=3764#article\",\"isPartOf\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=3764\"},\"author\":{\"name\":\"ponybai\",\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"headline\":\"XML \u652f\u6301\",\"datePublished\":\"2023-12-26T13:40:55+00:00\",\"dateModified\":\"2023-11-27T13:46:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=3764\"},\"wordCount\":165,\"publisher\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"keywords\":[\"Apex XML\"],\"articleSection\":[\"Apex \u5f00\u53d1\u4eba\u5458\u6307\u5357\"],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.ponybai.com\/?p=3764\",\"url\":\"http:\/\/www.ponybai.com\/?p=3764\",\"name\":\"XML \u652f\u6301 - \u767d\u767d\u968f\u7b14\",\"isPartOf\":{\"@id\":\"http:\/\/www.ponybai.com\/#website\"},\"datePublished\":\"2023-12-26T13:40:55+00:00\",\"dateModified\":\"2023-11-27T13:46:15+00:00\",\"description\":\"Apex \u63d0\u4f9b\u4e86\u5b9e\u7528\u7a0b\u5e8f\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790 \u4f7f\u7528\u6d41\u548c DOM \u7684 XML \u5185\u5bb9\u3002\",\"breadcrumb\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=3764#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.ponybai.com\/?p=3764\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.ponybai.com\/?p=3764#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/www.ponybai.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"XML \u652f\u6301\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.ponybai.com\/#website\",\"url\":\"http:\/\/www.ponybai.com\/\",\"name\":\"\u767d\u767d\u968f\u7b14\",\"description\":\"Salesforce\u5b98\u65b9\u8bb2\u5e08\",\"publisher\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.ponybai.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\",\"name\":\"ponybai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg\",\"contentUrl\":\"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg\",\"width\":248,\"height\":248,\"caption\":\"ponybai\"},\"logo\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/121.37.188.161\"],\"url\":\"http:\/\/www.ponybai.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"XML \u652f\u6301 - \u767d\u767d\u968f\u7b14","description":"Apex \u63d0\u4f9b\u4e86\u5b9e\u7528\u7a0b\u5e8f\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790 \u4f7f\u7528\u6d41\u548c DOM \u7684 XML \u5185\u5bb9\u3002","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.ponybai.com\/?p=3764","og_locale":"zh_CN","og_type":"article","og_title":"XML \u652f\u6301 - \u767d\u767d\u968f\u7b14","og_description":"Apex \u63d0\u4f9b\u4e86\u5b9e\u7528\u7a0b\u5e8f\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790 \u4f7f\u7528\u6d41\u548c DOM \u7684 XML \u5185\u5bb9\u3002","og_url":"http:\/\/www.ponybai.com\/?p=3764","og_site_name":"\u767d\u767d\u968f\u7b14","article_published_time":"2023-12-26T13:40:55+00:00","article_modified_time":"2023-11-27T13:46:15+00:00","og_image":[{"width":1372,"height":205,"url":"http:\/\/www.ponybai.com\/wp-content\/uploads\/2023\/11\/xml.png","type":"image\/png"}],"author":"ponybai","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"ponybai","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.ponybai.com\/?p=3764#article","isPartOf":{"@id":"http:\/\/www.ponybai.com\/?p=3764"},"author":{"name":"ponybai","@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"headline":"XML \u652f\u6301","datePublished":"2023-12-26T13:40:55+00:00","dateModified":"2023-11-27T13:46:15+00:00","mainEntityOfPage":{"@id":"http:\/\/www.ponybai.com\/?p=3764"},"wordCount":165,"publisher":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"keywords":["Apex XML"],"articleSection":["Apex \u5f00\u53d1\u4eba\u5458\u6307\u5357"],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/www.ponybai.com\/?p=3764","url":"http:\/\/www.ponybai.com\/?p=3764","name":"XML \u652f\u6301 - \u767d\u767d\u968f\u7b14","isPartOf":{"@id":"http:\/\/www.ponybai.com\/#website"},"datePublished":"2023-12-26T13:40:55+00:00","dateModified":"2023-11-27T13:46:15+00:00","description":"Apex \u63d0\u4f9b\u4e86\u5b9e\u7528\u7a0b\u5e8f\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790 \u4f7f\u7528\u6d41\u548c DOM \u7684 XML \u5185\u5bb9\u3002","breadcrumb":{"@id":"http:\/\/www.ponybai.com\/?p=3764#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.ponybai.com\/?p=3764"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.ponybai.com\/?p=3764#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/www.ponybai.com\/"},{"@type":"ListItem","position":2,"name":"XML \u652f\u6301"}]},{"@type":"WebSite","@id":"http:\/\/www.ponybai.com\/#website","url":"http:\/\/www.ponybai.com\/","name":"\u767d\u767d\u968f\u7b14","description":"Salesforce\u5b98\u65b9\u8bb2\u5e08","publisher":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.ponybai.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883","name":"ponybai","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/","url":"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg","contentUrl":"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg","width":248,"height":248,"caption":"ponybai"},"logo":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/121.37.188.161"],"url":"http:\/\/www.ponybai.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts\/3764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3764"}],"version-history":[{"count":1,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts\/3764\/revisions"}],"predecessor-version":[{"id":3766,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts\/3764\/revisions\/3766"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/media\/3765"}],"wp:attachment":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3764"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}