Xerces question
hi all,
i wanted to know how to get the attribute 'width' from the following tag:
<svg width="382" height="966" viewBox="0 0 382 966" xml:space="preserve">
also, i have an error(Exception in thread "main" java.lang.NoSuchMethodError: main ) when i tried to run my app even though i dont have any errors when compiling.
please help. thank you
the following is just a testing code(it does simple stuff):
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import java.io.File;
public class getImageAttribute
{
public getImageAttribute()
{
String txt;
Document doc;
Element imageElement;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse("d:\\project\\svg\\temp.svg");
Element root = doc.getDocumentElement();
imageElement = (Element)root.getElementsByTagName("image").item(0);
txt = imageElement.getAttribute("xlink:href");
System.out.println(txt);
}
catch (ParserConfigurationException ioe)
{
ioe.printStackTrace();
}
catch (SAXException sax)
{
sax.printStackTrace();
}
catch (IOException io)
{
io.printStackTrace();
}
}
}
[1536 byte] By [
xerokool] at [2007-9-26 2:17:39]

You get a runtime error, because you're trying to execute a main routine in a class that doesn't have any (any Java beginner's book would help).
For accessing the data in your attributes try the following:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;
public class JAXPDOMTest{
public void domParse(String url)
{
DocumentBuilder parser;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
try {
parser = factory.newDocumentBuilder();
Document doc = parser.parse(url);
Node svg = doc.getElementsByTagName("svg").item(0);
if(svg!=null)
{
NamedNodeMap nnmap = svg.getAttributes();
if(nnmap!=null)
{
int n=nnmap.getLength();
for(int i=0;i<n;++i)
{
Node node=nnmap.item(i);
System.out.println("attibute("+node.getNodeName()+")="+node.getNodeValue());
}
}
else
System.out.println("NamedNodeMap =null");
}
else
System.out.println("Node svg=null");
} catch (Exception e) {
e.printStackTrace();
}
}
static public void main(String[] args)
{
JAXPDOMTest x=new JAXPDOMTest2();
x.domParse("test.xml");
}
}
Here's the simple test.xml I tried:
><?xml version="1.0"?>
<svg width="382" height="966" viewBox="0 0 382 966" xml:space="preserve"/>
Take a look at this thread:
http://forums.java.sun.com/thread.jsp?forum=34&thread=151897
Hope that helps.
lk555 at 2007-6-29 9:17:36 >
