Menu
Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Java
Parsing XML with Dom
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="=?ISO-8859-1?Q?Arne_Vajh=F8j?=, post: 3079015"] Try look at the attached example. Arne ==================================== package september; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.traversal.DocumentTraversal; import org.w3c.dom.traversal.NodeFilter; import org.w3c.dom.traversal.TreeWalker; import org.xml.sax.InputSource; public class XMLandWS { public static void parse(String xml) throws Exception { System.out.print(xml); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(new StringReader(xml))); TreeWalker walk = ((DocumentTraversal) doc).createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_TEXT, null, false); Node n; while ((n = walk.nextNode()) != null) { System.out.println("=" + n.getNodeValue().replace("\n", "\\n").replace(" ", "_")); } } public static void main(String[] args) throws Exception { parse("<all>\n" + " <one>A</one>\n" + " <one>BB</one>\n" + " <one>CCC</one>\n" + "</all>\n"); parse("<!DOCTYPE all [\n" + "<!ELEMENT all (one)*>\n" + "<!ELEMENT one (#PCDATA)>\n" + "]>\n" + "<all>\n" + " <one>A</one>\n" + " <one>BB</one>\n" + " <one>CCC</one>\n" + "</all>\n"); parse("<!DOCTYPE all [\n" + "<!ELEMENT all (#PCDATA|one)*>\n" + "<!ELEMENT one (#PCDATA)>\n" + "]>\n" + "<all>\n" + " <one>A</one>\n" + " <one>BB</one>\n" + " <one>CCC</one>\n" + "</all>\n"); } } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Java
Parsing XML with Dom
Top