在Java中将XML作为字符串读取
有人能帮我吗。我想知道如何将这个示例理解为字符串?我知道如何读第一个,但不知道如何读所有的在Java中将XML作为字符串读取,java,xml,string,Java,Xml,String,有人能帮我吗。我想知道如何将这个示例理解为字符串?我知道如何读第一个,但不知道如何读所有的 <Tr rn=\"000000000000000\" vr=\"T\" sSpre=\"S\" reg=\"P\" dSpre=\"2000-01-01\" dOdprt=\"2000-01-01\" iban=\"SI00\" eno=\"R\" vir=\"B\" maticnaPps=\"0000000000\"><Imetnik davcna=\"00000000\" matSu
<Tr rn=\"000000000000000\" vr=\"T\" sSpre=\"S\" reg=\"P\" dSpre=\"2000-01-01\" dOdprt=\"2000-01-01\" iban=\"SI00\" eno=\"R\" vir=\"B\" maticnaPps=\"0000000000\"><Imetnik davcna=\"00000000\" matSub=\"0000000000\" drz=\"705\"><PopolnoIme>UNKNOWN</PopolnoIme><KratkoIme>UNKNOWN</KratkoIme><Naslov sifTipNaslova=\"00\" sifObcina=\"000\" sifPosta=\"0000\" sifUlica=\"0000\" sifNaselje=\"000\" stHisna=\"000\" sifHsmid=\"00000000\"><Obcina>UNKNOWN</Obcina><Posta>UNKNOWN</Posta><Ulica>UNKNOWN</Ulica><Naselje>UNKNOWN</Naselje></Naslov></Imetnik></Tr>
在Java中解析XML文件的最佳解决方案是使用专用库,如:
在Java中解析XML文件的最佳解决方案是使用专用库,如:
也许这就是你要找的?示例如下:
也许这就是你要找的?示例如下:
如果您的目标是从字符串对象加载/解析XML文档,那么您只需要使用通常的XML文档加载代码,但需要使用a来提供inputstream。或者ByteArrayInputStream,或者其他任何东西,只要建立一个转换链,允许您以输入流的形式访问数据 下面是一个未经测试且无异常处理的示例。抱歉,我目前没有测试环境:
final DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
final DocumentBuilder db = f.newDocumentBuilder();
final InputSource is = new InputSource();
is.setCharacterStream(new StringReader(YOURSTRING));
final Document doc = db.parse(is);
doc.getDocumentElement().normalize();
/*
* do whatever you want/need here.
*/
如果这不是您想要的,很抱歉,我不太清楚您在这里问的是什么。如果您的目标是从字符串对象加载/解析XML文档,您只需要使用通常的XML文档加载代码,但需要使用a来提供输入流。或者ByteArrayInputStream,或者其他任何东西,只要建立一个转换链,允许您以输入流的形式访问数据 下面是一个未经测试且无异常处理的示例。抱歉,我目前没有测试环境:
final DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
final DocumentBuilder db = f.newDocumentBuilder();
final InputSource is = new InputSource();
is.setCharacterStream(new StringReader(YOURSTRING));
final Document doc = db.parse(is);
doc.getDocumentElement().normalize();
/*
* do whatever you want/need here.
*/
如果这不是您想要的,对不起,我不太清楚您在这里问什么。使用xerces可能更容易理解:
public static void loadImetniks(String filePath) {
File xmlFile;
SAXBuilder builder;
Element root, child;
Imetnik imet;//another class that you have to create to help you for parsing
Document doc;
try {
xmlFile = new File(filePath);
builder = new SAXBuilder(); // parameters control validation, etc
doc = builder.build(xmlFile);
root = doc.getRootElement(); // Tr could be the root but I am not sure if you will have more Tr nodes in the same file??
tr.setRn(root.getAttributeValue(Constants.RN));//define the constants string in another file
tr.setVr(root.getAttributeValue(Constants.VR));
tr.setSspre(root.getAttributeValue(Constants.SSPRE));
tr.setReg(root.getAttributeValue(Constants.REG));
tr.setIban(root.getAttributeValue(Constants.IBAN));
.... //repeat for every attribute
....
List children = root.getChildren(); // depends of how many Imetnik you will have
for (Iterator iter = children.iterator(); iter.hasNext();) {
child = (Element) iter.next();
imet = new Imetnik();
imet.loadXML(child); // you have to define the loadXML function in your object Imetnik which should extract the attributes and internal nodes
//imets.add(contest); // just use in the case that you will have to extract more than one Imetnik node
}
} catch (Exception e) {
log.error("Error al hacer el parsing del contests.xml!");
log.error(e.getMessage());
}
}
例如,您的imenik类应该包含:
public void loadXML(Element root) {
Element child;
//Naslov naslov; // for Naslov because it could be an object itself
davcna = root.getAttributeValue(Constants.DAVCNA); //define the string constant
matSub = root.getAttributeValue(Constants.MATSUB); //define the string constant
drz = Integer.parseInt(root.getAttributeValue(Constants.DRZ)); //define the string constant
List children = root.getChildren(); // your root is Imetnik now
for (Iterator iter = children.iterator(); iter.hasNext();) {
.....
.......
}
}
使用xerces可能更容易理解:
public static void loadImetniks(String filePath) {
File xmlFile;
SAXBuilder builder;
Element root, child;
Imetnik imet;//another class that you have to create to help you for parsing
Document doc;
try {
xmlFile = new File(filePath);
builder = new SAXBuilder(); // parameters control validation, etc
doc = builder.build(xmlFile);
root = doc.getRootElement(); // Tr could be the root but I am not sure if you will have more Tr nodes in the same file??
tr.setRn(root.getAttributeValue(Constants.RN));//define the constants string in another file
tr.setVr(root.getAttributeValue(Constants.VR));
tr.setSspre(root.getAttributeValue(Constants.SSPRE));
tr.setReg(root.getAttributeValue(Constants.REG));
tr.setIban(root.getAttributeValue(Constants.IBAN));
.... //repeat for every attribute
....
List children = root.getChildren(); // depends of how many Imetnik you will have
for (Iterator iter = children.iterator(); iter.hasNext();) {
child = (Element) iter.next();
imet = new Imetnik();
imet.loadXML(child); // you have to define the loadXML function in your object Imetnik which should extract the attributes and internal nodes
//imets.add(contest); // just use in the case that you will have to extract more than one Imetnik node
}
} catch (Exception e) {
log.error("Error al hacer el parsing del contests.xml!");
log.error(e.getMessage());
}
}
例如,您的imenik类应该包含:
public void loadXML(Element root) {
Element child;
//Naslov naslov; // for Naslov because it could be an object itself
davcna = root.getAttributeValue(Constants.DAVCNA); //define the string constant
matSub = root.getAttributeValue(Constants.MATSUB); //define the string constant
drz = Integer.parseInt(root.getAttributeValue(Constants.DRZ)); //define the string constant
List children = root.getChildren(); // your root is Imetnik now
for (Iterator iter = children.iterator(); iter.hasNext();) {
.....
.......
}
}
你把它读成字符串是什么意思?因为这个内容是在一个.xml文件中,所以我想用java来读取它,并把它放在一个字符串中?顺便说一句,看起来就像这个例子本身,所有的“\”都是java字符串。你想把xml读成字符串还是把Tr元素的属性值读成字符串?我想他/她是指从字符串对象解析xml文档。在这种情况下,它是一天结束时,您真正需要的是Document Document=builder.parsenew InputSourcenew StringReaderxmlString;。你把它读成字符串是什么意思?因为这个内容是在一个.xml文件中,所以我想用java来读取它,并把它放在一个字符串中?顺便说一句,看起来就像这个例子本身,所有的“\”都是java字符串。你想把xml读成字符串还是把Tr元素的属性值读成字符串?我想他/她是指从字符串对象解析xml文档。在这种情况下,它是一天结束时,您真正需要的是Document Document=builder.parsenew InputSourcenew StringReaderxmlString;。我放了\这样java就不会把它算作注释了。是的,我想把这行读成字符串。这不是一个很有用的答案。也许您提供了一点代码来启动操作。或者指向一个好的教程。我放了\这样java就不会把它算作注释了。是的,我想把这行读成字符串。这不是一个很有用的答案。也许您提供了一点代码来启动操作。或者指向一个好的教程。非常接近,是的。我将尝试从头开始复制/粘贴输出,以查看输出的格式。rn=0310021626306我只是不知道如何滚动我的抓取来阅读整行内容。非常接近,是的。我将尝试从头开始复制/粘贴输出,以查看输出的格式。rn=0310021626306我只是不知道如何滚动我的草稿来阅读整行内容。