Results 1 to 4 of 4
Thread: xml parser (2)
- 01-25-2012, 01:26 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
xml parser (2)
My second post on the topic, I've created a class that will handle xml files for a larger project I am working on. The parse method I have been working on has been working fine. So in this method
is a privateJava Code:doc
variable that has already been parsed. What I'm looking to do is get each one of these attributes in the list, which I have managed to do. However, they are ordered differently in the list than they are in the xml file I read them from. It just seems to be a random order, which isn't good since eventually I'm looking to take the attributes and use them to populate some fields. Is there any way around this? One way I thought of was to get each attribute individually, but how would I do that if the Node api includes a getAtributtes method but not getAttribute...?Java Code:Document
xml code concerned:Java Code:public String[] getValues(String tagName, int amount) { String[] s1 = new String[2]; String[] s2 = new String[100]; doc.normalizeDocument(); Element rootElement = doc.getDocumentElement(); NodeList subElement = rootElement.getElementsByTagName(tagName); Node element = subElement.item(0); NamedNodeMap attributes = element.getAttributes(); for(int i = 0; i < amount; i++) { s1 = (attributes.item(i).toString()).split("\""); s2[i] = (s1[1]); } return s2; }
XML Code:<?xml version="1.0" encoding="us-ascii"?> <ConfigurationData> <CommunicationParameters Port="COM1" Baud="115200" PacketFrequency="5.0" PacketTimeout="1.0" CommunicationsType="1" UserFrequency="5.0" UserTimeout="1.0" NamedPort="" /> <ConfigurationParameters Filename="C:\Documents and Settings\Tim Cowen.EXTORR-TIM.000\Desktop\shipped\in progress\sn4118_Repair\sn4118_Repair_factory_cal.cfg" Description="Air + PTA" TimeStamp="07d9000600010008000b0035003601e8" DateTime="6/8/2009 11:53:54 AM" Caption="4118 Repair" Serial="0" Model="0" /> <ScanParameters ScanSpeed="144" LowMass="1" HighMass="110" SamplesPerAMU="10" />
- 01-25-2012, 09:53 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: xml parser (2)
You want a Map<String, String> to map the attribute name to it's value.
Return that, then the calling method can turn that into an appropriate object (eg a CommunicationParameters object, or a ConfigurationParameters object).
- 01-26-2012, 05:02 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Re: xml parser (2)
Just to make sure, I'm assuming the attribute name is, for example, "SamplesPerAMU" ?
- 01-26-2012, 09:47 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
xml parser
By kkGG in forum Advanced JavaReplies: 7Last Post: 01-25-2012, 09:46 AM -
XML parser
By Lund01 in forum New To JavaReplies: 5Last Post: 03-21-2011, 04:50 PM -
Parser API
By sruti_mohan in forum Advanced JavaReplies: 0Last Post: 06-09-2008, 07:23 AM -
JSP Parser????
By chathu03j in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-10-2008, 12:08 PM -
DNS name parser 1.2.1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-14-2007, 08:21 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks