Hi,
Here is a snippet of a java cfx tag i wrote to parse an xml document. Your
code will depend on the structure of the xml file, but this might help to
get you started.
<!--- snip --->
String myVal;
// try to parse xml document then output data
try {
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse (new
InputSource("c:\myXMLDoc.xml"));
Element element = document.getDocumentElement();
NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE) {
NodeList nodes2 = node.getChildNodes();
for (int a = 0; a < nodes2.getLength(); a++) {
Node node2 = nodes2.item(a);
if(node2.getNodeType() == Node.ELEMENT_NODE) {
if (node2.hasChildNodes()) {
myVal =
node2.getFirstChild().getNodeValue();
} else {
myVal = "NULL";
}
}
}
}
}
}
<!--- end of snip --->
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
> </head>
> <body bgcolor="#ffffff" text="#000000">
> I guess I should access the text nodes as children of the items I'm
> accessing?<br>
> <br>
> ie.<br>
> <br>
> Element.getChildNodes()<br>
> <br>
> and make sure the nodes are? does whitespace come back as a TEXT_NODE?<br>
> <br>
> Node.TEXT_NODE<br>
> <br>
> Cheers.<br>
> <br>
> Gareth.<br>
> <br>
> Mark Mandel wrote:
> <blockquote
> cite="[EMAIL PROTECTED]"
> type="cite">
> <pre wrap="">Well there are 2 things at play here -
>
> 1) Elements don't neccessarily have text (you may want a TEXT_NODE)
> 2) getTextContent() is a Java 1.5 method, so you could either have (a)
> the wrong JDK version or (b) it may not be implemented on the XML
> parser you are using.
>
> Enjoy :o)
>
> Mark
>
> On 8/12/05, Gareth Edwards <a class="moz-txt-link-rfc2396E"
> href="mailto:[EMAIL PROTECTED]"><[EMAIL PROTECTED]></a> wrote:
> </pre>
> <blockquote type="cite">
> <pre wrap="">
> 500 org.w3c.dom.Node.getTextContent()Ljava/lang/String;
> org.w3c.dom.Node.getTextContent()Ljava/lang/String;
>
>
> Mark Mandel wrote:
> What's the error gareth?
>
> (and why you creating a new String()... seems kinda redundant)
>
> Mark
>
> On 8/12/05, Gareth Edwards <a class="moz-txt-link-rfc2396E"
> href="mailto:[EMAIL PROTECTED]"><[EMAIL PROTECTED]></a> wrote:
>
>
> I'm using Java to parse an xml document and have included some bits of the
> method that helps me describe what is causing a problem.
>
> import org.w3c.dom.*;
> import javax.xml.parsers.DocumentBuilder;
> import javax.xml.parsers.DocumentBuilderFactory;
>
> The bit below is what I'm a little confused about, it seems that the line
> hashvalue = new String(nodelist.item(nodeindex).getTextContent());
> seems to throw a strange Java servlet / string error, anyone know what
> might be causing this?
>
> for(int nodeindex = 0; nodeindex < nodelist.getLength(); nodeindex++){
> if(nodelist.item(nodeindex).getNodeType() == Node.ELEMENT_NODE)
> {
> hashkey = new
> String(nodelist.item(nodeindex).getNodeName()).trim();
> hashvalue = new
> String(nodelist.item(nodeindex).getTextContent());
> properties.put(hashkey,hashvalue);
> }
> }
>
>
>
> --
>
>
>
>
>
> the alpha school system PTY LTD
> Unit 1, 31 Thompson Street Bowen Hills QLD 4006
> T Intl +61 7 3216 0999 F Intl +61 7 3216 0500
> <a class="moz-txt-link-abbreviated"
> href="http://www.tassweb.com.au">www.tassweb.com.au</a> ABN 87 010 874 816
>
>
> TASS news
>
> - Change of company name - the alpha school system PTY LTD
>
> - New & improved website coming soon
>
> - TASS.web Fundraising upgrade available now!
>
> - TASS 6.09.007 patch release ready for download
>
> ----- Visit <a class="moz-txt-link-abbreviated"
> href="http://www.tassweb.com.au">www.tassweb.com.au</a> for more info -------
>
>
>
> Virus Disclaimer: Please note that this email may contain computer-generated
> file(s) as an attachment. Whereas we take every effort to protect our files
> from computer virus, we recommend that you check the file(s) with your own
> virus detection software prior to opening it. If you would prefer to receive
> a hardcopy of the file please email or phone. Alpha Business Systems does
> not accept liability for the consequences of any computer viruses that may
> be transmitted with this email.
>
> Confidentiality: This email and any attached files are intended solely for
> the addressee(s) and may contain confidential and privileged information. If
> you received this email in error, please advise us by return email or phone
> and disregard the contents and immediately delete it and destroy any copies.
>
>
>
>
> ---
> You are currently subscribed to cfaussie as: <a
> class="moz-txt-link-abbreviated" href="mailto:[EMAIL PROTECTED]">[EMAIL
> PROTECTED]</a>
> To unsubscribe send a blank email to
> <a class="moz-txt-link-abbreviated" href="mailto:[EMAIL PROTECTED]">[EMAIL
> PROTECTED]</a> Aussie Macromedia
> Developers: <a class="moz-txt-link-freetext"
> href="http://lists.daemon.com.au/">http://lists.daemon.com.au/</a>
> </pre>
> </blockquote>
> <pre wrap=""><!---->
> </pre>
> </blockquote>
> </body>
> </html>
---
You are currently subscribed to cfaussie as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]
Aussie Macromedia Developers: http://lists.daemon.com.au/