如何在java中获取XML的价值
如何在java中解析这种类型的XML 示例如何在java中获取XML的价值,java,xml,Java,Xml,如何在java中解析这种类型的XML 示例 <?xml version="1.0" encoding="UTF-8" ?> <isomsg direction="incoming"> <id id="2" value="929501003300038807"/> <id id="3" value="0301100"/> <id id="4" value="000000000000"/> </isomsg>
<?xml version="1.0" encoding="UTF-8" ?>
<isomsg direction="incoming">
<id id="2" value="929501003300038807"/>
<id id="3" value="0301100"/>
<id id="4" value="000000000000"/>
</isomsg>
现在,我想获得值
9295010033000383807030110000000000000000
这里是一个使用jaxb的简单示例
假设您有以下XML,请注意我更正了
<id="2" value="929501003300038807"/> to
<?xml version="1.0" encoding="UTF-8" ?>
<isomsg direction="incoming">
<id id="2" value="929501003300038807"/>
<id id="3" value="0301100"/>
<id id="4" value="000000000000"/>
</isomsg>
XMLRunner来测试您的课程
@XmlRootElement(name = "isomsg")
@XmlAccessorType(XmlAccessType.FIELD)
public class Isomsg
{
@XmlElement(name = "id")
private List<XMLObject> idList = null;
public List<XMLObject> getIdList()
{
return idList;
}
public void setIdList(List<XMLObject> idList)
{
this.idList = idList;
}
}
public class XMLRunner
{
public static void main(String[] args)
{
doFromFile("d:\\myxml.xml");
doFromString("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
" <isomsg direction=\"incoming\">\n" +
" <id id=\"2\" value=\"929501003300038807\"/>\n" +
" <id id=\"3\" value=\"0301100\"/>\n" +
" <id id=\"4\" value=\"000000000000\"/>\n" +
" </isomsg> ");
}
public static void doFromFile(String filename)
{
try
{
System.out.println("About to unmarshal from file : " + filename);
File file = new File(filename);
JAXBContext jaxbContext = JAXBContext.newInstance(Isomsg.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Isomsg isomsg = (Isomsg) jaxbUnmarshaller.unmarshal(file);
System.out.println("You have " + isomsg.getIdList().size() + " items");
for (XMLObject xmlObject : isomsg.getIdList())
{
System.out.println(xmlObject.getValue());
}
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
public static void doFromString(String xmlString)
{
try
{
System.out.println("About to unmarshal from string : " + xmlString);
JAXBContext jaxbContext = JAXBContext.newInstance(Isomsg.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xmlString);
Isomsg isomsg = (Isomsg) jaxbUnmarshaller.unmarshal(reader);
System.out.println("You have " + isomsg.getIdList().size() + " items");
for (XMLObject xmlObject : isomsg.getIdList())
{
System.out.println(xmlObject.getValue());
}
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
公共类XMLRunner
{
公共静态void main(字符串[]args)
{
doFromFile(“d:\\myxml.xml”);
doFromString(“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
" ");
}
公共静态void doFromFile(字符串文件名)
{
尝试
{
System.out.println(“即将从文件中解组:“+filename”);
文件=新文件(文件名);
JAXBContext JAXBContext=JAXBContext.newInstance(Isomsg.class);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
Isomsg Isomsg=(Isomsg)jaxbUnmarshaller.unmarshal(文件);
System.out.println(“您有”+isomsg.getIdList().size()+items”);
for(XMLObject:isomsg.getIdList())
{
System.out.println(xmlObject.getValue());
}
}
捕获(JAXBEException e)
{
e、 printStackTrace();
}
}
公共静态void doFromString(String xmlString)
{
尝试
{
System.out.println(“即将从字符串中解组:“+xmlString”);
JAXBContext JAXBContext=JAXBContext.newInstance(Isomsg.class);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
StringReader=新的StringReader(xmlString);
Isomsg Isomsg=(Isomsg)jaxbUnmarshaller.unmarshal(reader);
System.out.println(“您有”+isomsg.getIdList().size()+items”);
for(XMLObject:isomsg.getIdList())
{
System.out.println(xmlObject.getValue());
}
}
捕获(JAXBEException e)
{
e、 printStackTrace();
}
}
}
帮助链接您的xml无效id=“2
不是有效的属性实际上我想从字符串而不是文件中解析此xml,如何解决此问题。@KawshikRoy,我已经编辑了我的帖子,方法doFromString()将从字符串中解组谢谢@Kenneth Clark:)
public class XMLRunner
{
public static void main(String[] args)
{
doFromFile("d:\\myxml.xml");
doFromString("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
" <isomsg direction=\"incoming\">\n" +
" <id id=\"2\" value=\"929501003300038807\"/>\n" +
" <id id=\"3\" value=\"0301100\"/>\n" +
" <id id=\"4\" value=\"000000000000\"/>\n" +
" </isomsg> ");
}
public static void doFromFile(String filename)
{
try
{
System.out.println("About to unmarshal from file : " + filename);
File file = new File(filename);
JAXBContext jaxbContext = JAXBContext.newInstance(Isomsg.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Isomsg isomsg = (Isomsg) jaxbUnmarshaller.unmarshal(file);
System.out.println("You have " + isomsg.getIdList().size() + " items");
for (XMLObject xmlObject : isomsg.getIdList())
{
System.out.println(xmlObject.getValue());
}
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
public static void doFromString(String xmlString)
{
try
{
System.out.println("About to unmarshal from string : " + xmlString);
JAXBContext jaxbContext = JAXBContext.newInstance(Isomsg.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xmlString);
Isomsg isomsg = (Isomsg) jaxbUnmarshaller.unmarshal(reader);
System.out.println("You have " + isomsg.getIdList().size() + " items");
for (XMLObject xmlObject : isomsg.getIdList())
{
System.out.println(xmlObject.getValue());
}
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}