Well it's been a little while since I've done XML manipulation in Java, but I remember when I did something similar to this, only my source was over a socket instead of from a file, but that should be irrelevant, you'll have to do something like this to turn your XML into a Document type, which can be parsed from there.
Code:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(xmlSchema))); //where xmlSchema is my String
traverseSchema(doc); //my function to parse the state machine represented by XML
So now that you're here, you will have Node and NodeList manipulation, based on what you're looking for. I had many element which had attributes like yours, and I was looking for occurences of them.
But given your structure, say you have your Node (a Node will be in your case, a single one of your Nodes in your example), you could obtain the attributes by
Code:
Node myNode;
... //some extraction of your Node
myNode.getAttributes().getNamedItem("name").getNodeValue(); //returns name1
I should mention that I was using the following classes:
You can do it two main ways - either with DOM (Document Object Model), which loads the whole XML file as a Document, or with SAX (Simple API for XML), which reads the file and calls into your code with details of each element.
DOM is easier to use for small files, and gives you 'random access' to the elements, but takes a while and a lot of memory to load large files. Sax is a bit fiddly to use, but relatively lightweight and fast, and better suited sequential processing of large files.
The ability to simplify means to eliminate the unnecessary so that the necessary may speak...
H. Hofmann
Last edited by dlorde; June 6th, 2008 at 06:56 PM.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Well it's been a little while since I've done XML manipulation in Java, but I remember when I did something similar to this, only my source was over a socket instead of from a file, but that should be irrelevant, you'll have to do something like this to turn your XML into a Document type, which can be parsed from there.
Code:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(xmlSchema))); //where xmlSchema is my String
traverseSchema(doc); //my function to parse the state machine represented by XML
So now that you're here, you will have Node and NodeList manipulation, based on what you're looking for. I had many element which had attributes like yours, and I was looking for occurences of them.
But given your structure, say you have your Node (a Node will be in your case, a single one of your Nodes in your example), you could obtain the attributes by
Code:
Node myNode;
... //some extraction of your Node
myNode.getAttributes().getNamedItem("name").getNodeValue(); //returns name1
I should mention that I was using the following classes:
If you're still stuck, just reply I'll see if I can give you a more concrete example if it's required. The Java API describes all this.
hi,thx a lot for your solution, will try this out and let you know.
I have got many solution on net for nodes like
<a>xyz</a> but in my case i had more than one value : <a id=1 name=n> and some thing like that. for that i was looking for here. And i hope that your code snippet will work for my case.
You can do it two main ways - either with DOM (Document Object Model), which loads the whole XML file as a Document, or with SAX (Simple API for XML), which reads the file and calls into your code with details of each element.
DOM is easier to use for small files, and gives you 'random access' to the elements, but takes a while and a lot of memory to load large files. Sax is a bit fiddly to use, but relatively lightweight and fast, and better suited sequential processing of large files.
The ability to simplify means to eliminate the unnecessary so that the necessary may speak...
H. Hofmann
Thanks for reply, but i have came across this solution before posting this question. But in this , the node is having single value. and in my case node is havingmore than one attribute. So, how can i access multiple attributes using this solution ?
Thanks for reply, but i have came across this solution before posting this question. But in this , the node is having single value. and in my case node is havingmore than one attribute. So, how can i access multiple attributes using this solution ?
I posted two possible solutions, so it would be helpful if you said which one you're talking about, but I'm going to assume it's the SAX solution. If you look at the example (or, indeed, the SAX API docs), you'll see that the content handler startElement method callback takes an Attributes parameter. These are the attributes in the current element. You can get the value of an attribute out of the Attributes object like this:
SAX serializes the XML tree structure, simply calling startElement(..) and endElement(..) for each element.
Today, most software exists, not to solve a problem, but to interface with other software...
I.O. Angell
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
I posted two possible solutions, so it would be helpful if you said which one you're talking about, but I'm going to assume it's the SAX solution. If you look at the example (or, indeed, the SAX API docs), you'll see that the content handler startElement method callback takes an Attributes parameter. These are the attributes in the current element. You can get the value of an attribute out of the Attributes object like this:
Well it's been a little while since I've done XML manipulation in Java, but I remember when I did something similar to this, only my source was over a socket instead of from a file, but that should be irrelevant, you'll have to do something like this to turn your XML into a Document type, which can be parsed from there.
Code:
(xmlSchema))); //where xmlSchema is my String
traverseSchema(doc); //my function to parse the state machine represented by XML
Code:
Node myNode;
... //some extraction of your Node
myNode.getAttributes().getNamedItem("name").getNodeValue(); //returns name1
can you please tell me what exactly your traverseSchema(doc); function does? whats code underlying? and for getting node attribute value u mention one code snippet. So, how i will get NODE element value?
you may be finding my question too silly, but i am really new to this XML READING thing.
and will it traverse node one by one ?because i want find the parent of the given attribute value.
my node structure :
....
<node id="1" name="n1" parent="p1">
<node id="2" name="n2" parent="p2">
......
my function will be like this :
if I pass "n1" it should return the "p1". and if "n2" then "p2" should be returned.
means, on passing the value of one node attribute's value, it should return the value of other attributes value , and that of the same node.
can you please tell me what exactly your traverseSchema(doc); function does? whats code underlying? and for getting node attribute value u mention one code snippet. So, how i will get NODE element value?
you may be finding my question too silly, but i am really new to this XML READING thing.
and will it traverse node one by one ?because i want find the parent of the given attribute value.
my node structure :
....
<node id="1" name="n1" parent="p1">
<node id="2" name="n2" parent="p2">
......
my function will be like this :
if I pass "n1" it should return the "p1". and if "n2" then "p2" should be returned.
means, on passing the value of one node attribute's value, it should return the value of other attributes value , and that of the same node.
I posted two possible solutions, so it would be helpful if you said which one you're talking about, but I'm going to assume it's the SAX solution. If you look at the example (or, indeed, the SAX API docs), you'll see that the content handler startElement method callback takes an Attributes parameter. These are the attributes in the current element. You can get the value of an attribute out of the Attributes object like this:
Well it's been a little while since I've done XML manipulation in Java, but I remember when I did something similar to this, only my source was over a socket instead of from a file, but that should be irrelevant, you'll have to do something like this to turn your XML into a Document type, which can be parsed from there.
Code:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(xmlSchema))); //where xmlSchema is my String
traverseSchema(doc); //my function to parse the state machine represented by XML
So now that you're here, you will have Node and NodeList manipulation, based on what you're looking for. I had many element which had attributes like yours, and I was looking for occurences of them.
But given your structure, say you have your Node (a Node will be in your case, a single one of your Nodes in your example), you could obtain the attributes by
Code:
Node myNode;
... //some extraction of your Node
myNode.getAttributes().getNamedItem("name").getNodeValue(); //returns name1
I should mention that I was using the following classes:
Hi, I recently implemented this for my Chess program that i have been writing on and off whenever i can be bothered.
Fire this code into Eclipse and add the jar file to the classpath.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.