This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Need help for convert xml by xstream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need help for convert xml by xstream" Watch "Need help for convert xml by xstream" New topic
Author

Need help for convert xml by xstream

Haulyn Jason
Ranch Hand

Joined: Jul 24, 2007
Posts: 37
Hi, all:
I have an XML file, format as follow:
<actions>
<action>
<path>admin.do</path>
<type>0</path>
</action>

<action>
<path>memberProfile.do</path>
<type>0</path>
</action>

<action>
<path>memberAccount.do</path>
<type>1</path>
</action>
</actions>


I want to convert to my javabean, and bind <action> node as Map, I mean <path> node value is the key and <type>node value is the value for HashMap<String,Long>();

Can anyone help me? and, if xstream is not suitable, other way is ok. I just need to convert the xml to a map. If it is necessary, I can change the format of xml.

Thanks!


Mama always said, "Life is like a box of chocolates; you never know what you're gonna get. And then you have to get medieval on somebody's buttocks."
http://www.haulynjason.net/
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<path>admin.do</path>
<type>0</type>
</action>
<action>
<path>memberProfile.do</path>
<type>0</type>
</action>
<action>
<path>memberAccount.do</path>
<type>1</type>
</action>
</actions>
------------------------------------------------------------------------------------------------------------------------------------------------------

package pullparser;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class ActionMapper {

/**
* @param args
*/
public static void main(String[] args) {
XmlPullParserFactory factory=null;
Map<String,Long> aMap = new HashMap<String,Long>();
try {
factory = XmlPullParserFactory.newInstance();
} catch (XmlPullParserException e) {
e.printStackTrace();
}

FileReader fr = null;
XmlPullParser parser=null;
try {
fr=new FileReader("c:\\actions.xml");
parser = factory.newPullParser();
parser.setInput(fr);
int eventType =0;
eventType = parser.getEventType();
String tagName="";
String path="";
String type="";
while(eventType!=XmlPullParser.END_DOCUMENT) {
if(eventType==XmlPullParser.START_TAG) {
tagName = parser.getName();
}else if(eventType==XmlPullParser.TEXT) {
if(parser.getText().trim().length()==0){

}else if(tagName.equals("path")) {
path =parser.getText();
}else if(tagName.equals("type")) {
type = parser.getText();
aMap.put(path, new Long(type));
}
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}
System.out.println(aMap);
}
}


cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help for convert xml by xstream
 
Similar Threads
help! struts in Borland JBuilder8.0
Castor unable to Map - please help
Xstream xml from toXML give problem to display in javscript
Problem in castor mapping file
Reading values from xml file