正在处理 xml 个申请
Processing xml application
我必须创建一个 java 应用程序来处理 xml 个文件。确切地说,应用程序读取了一个包含多个供应商的订单 xml 文件。
例如,有一个订单有 2 件产品来自 Apple,2 件来自三星,1 件来自诺基亚。
应用程序必须为每个供应商创建 1 个 xml 文件。我应该 apple.xml 苹果产品(来自 order.xml),samsung.xml 三星产品和 nokia.xml 诺基亚产品。
我能够读取 order.xml,将每个节点标签存储到一个变量中,所以我有一个变量 'price'、'description'、'supplier' 等。
但我不知道如何为每个供应商创建一个新的 xml,以及如何存储唯一的供应商名称。因为如果我有 3 个 Apple 产品,需要 'Apple' 3 次。
然后为每个供应商出口一个xml供应商的产品。
非常感谢!
您是否尝试使用 XML 解析器?它可以帮助您以一种简单的方式读写 XML 文件。这里有两个例子:
有几种方法可以构建 xml。我这里只有两个例子。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateXMLClass {
public CreateXMLClass() {
String xml1 = getXML1(new File("sample.xml"));
System.out.println(xml1);
System.out.println("");
System.out.println("");
String xml2 = getXML2();
System.out.println(xml2);
}
// creates xml file and returns the contents
private String getXML1(File f) {
String data="";
TransformerFactory transformerFactory;
Transformer transformer;
DOMSource source;
StreamResult result;
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root elements
Document doc = docBuilder.newDocument();
Element soapEnvelope = doc.createElement("soapenv:Envelope");
soapEnvelope.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
soapEnvelope.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
soapEnvelope.setAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
soapEnvelope.setAttribute("xmlns:ns0", "https://ws.mycompany.com/");
doc.appendChild(soapEnvelope);
//elements
Element header = doc.createElement("soapenv:Header");
soapEnvelope.appendChild(header);
Element soapBody = doc.createElement("soapenv:Body");
soapEnvelope.appendChild(soapBody);
Element ns0 = doc.createElement("ns0:SampleApplication");
ns0.setAttribute("xmlns:ns0", "http://ws.mycompany.com");
soapBody.appendChild(ns0);
Element element01 = doc.createElement("Sample1");
element01.appendChild(doc.createTextNode("value1"));
ns0.appendChild(element01);
Element element02 = doc.createElement("Sample2");
element02.appendChild(doc.createTextNode("value2"));
ns0.appendChild(element02);
Element element03 = doc.createElement("Sample3");
element03.appendChild(doc.createTextNode("value3"));
ns0.appendChild(element03);
//write the content into xml file
transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
source = new DOMSource(doc);
result = new StreamResult(f);
transformer.transform(source, result);
FileReader fr;
fr = new FileReader(f);
int d;
d = fr.read();
while (d != -1) {
data = data +(char)d;
d = fr.read();
}
fr.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch(ParserConfigurationException pce){
pce.printStackTrace();
} catch(TransformerException tfe){
tfe.printStackTrace();
}
return data;
}
// creates xml and returns the content
private String getXML2() {
String data="";
TransformerFactory transformerFactory;
Transformer transformer;
DOMSource source;
StreamResult result;
StringWriter writer;
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root elements
Document doc = docBuilder.newDocument();
Element soapEnvelope = doc.createElement("soapenv:Envelope");
soapEnvelope.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
soapEnvelope.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
soapEnvelope.setAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
soapEnvelope.setAttribute("xmlns:ns0", "https://ws.mycompany.com/");
doc.appendChild(soapEnvelope);
//elements
Element header = doc.createElement("soapenv:Header");
soapEnvelope.appendChild(header);
Element soapBody = doc.createElement("soapenv:Body");
soapEnvelope.appendChild(soapBody);
Element ns0 = doc.createElement("ns0:SampleApplication");
ns0.setAttribute("xmlns:ns0", "http://ws.mycompany.com");
soapBody.appendChild(ns0);
Element element01 = doc.createElement("Sample1");
element01.appendChild(doc.createTextNode("value1"));
ns0.appendChild(element01);
Element element02 = doc.createElement("Sample2");
element02.appendChild(doc.createTextNode("value2"));
ns0.appendChild(element02);
Element element03 = doc.createElement("Sample3");
element03.appendChild(doc.createTextNode("value3"));
ns0.appendChild(element03);
writer = new StringWriter();
result = new StreamResult(writer);
transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
source = new DOMSource(doc);
transformer.transform(source, result);
data = writer.getBuffer().toString();
} catch(ParserConfigurationException pce){
pce.printStackTrace();
} catch(TransformerException tfe){
tfe.printStackTrace();
}
return data;
}
public static void main(String args[]) {
new CreateXMLClass();
System.exit(0);
}
}
我必须创建一个 java 应用程序来处理 xml 个文件。确切地说,应用程序读取了一个包含多个供应商的订单 xml 文件。
例如,有一个订单有 2 件产品来自 Apple,2 件来自三星,1 件来自诺基亚。
应用程序必须为每个供应商创建 1 个 xml 文件。我应该 apple.xml 苹果产品(来自 order.xml),samsung.xml 三星产品和 nokia.xml 诺基亚产品。
我能够读取 order.xml,将每个节点标签存储到一个变量中,所以我有一个变量 'price'、'description'、'supplier' 等。
但我不知道如何为每个供应商创建一个新的 xml,以及如何存储唯一的供应商名称。因为如果我有 3 个 Apple 产品,需要 'Apple' 3 次。
然后为每个供应商出口一个xml供应商的产品。
非常感谢!
您是否尝试使用 XML 解析器?它可以帮助您以一种简单的方式读写 XML 文件。这里有两个例子:
有几种方法可以构建 xml。我这里只有两个例子。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateXMLClass {
public CreateXMLClass() {
String xml1 = getXML1(new File("sample.xml"));
System.out.println(xml1);
System.out.println("");
System.out.println("");
String xml2 = getXML2();
System.out.println(xml2);
}
// creates xml file and returns the contents
private String getXML1(File f) {
String data="";
TransformerFactory transformerFactory;
Transformer transformer;
DOMSource source;
StreamResult result;
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root elements
Document doc = docBuilder.newDocument();
Element soapEnvelope = doc.createElement("soapenv:Envelope");
soapEnvelope.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
soapEnvelope.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
soapEnvelope.setAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
soapEnvelope.setAttribute("xmlns:ns0", "https://ws.mycompany.com/");
doc.appendChild(soapEnvelope);
//elements
Element header = doc.createElement("soapenv:Header");
soapEnvelope.appendChild(header);
Element soapBody = doc.createElement("soapenv:Body");
soapEnvelope.appendChild(soapBody);
Element ns0 = doc.createElement("ns0:SampleApplication");
ns0.setAttribute("xmlns:ns0", "http://ws.mycompany.com");
soapBody.appendChild(ns0);
Element element01 = doc.createElement("Sample1");
element01.appendChild(doc.createTextNode("value1"));
ns0.appendChild(element01);
Element element02 = doc.createElement("Sample2");
element02.appendChild(doc.createTextNode("value2"));
ns0.appendChild(element02);
Element element03 = doc.createElement("Sample3");
element03.appendChild(doc.createTextNode("value3"));
ns0.appendChild(element03);
//write the content into xml file
transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
source = new DOMSource(doc);
result = new StreamResult(f);
transformer.transform(source, result);
FileReader fr;
fr = new FileReader(f);
int d;
d = fr.read();
while (d != -1) {
data = data +(char)d;
d = fr.read();
}
fr.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch(ParserConfigurationException pce){
pce.printStackTrace();
} catch(TransformerException tfe){
tfe.printStackTrace();
}
return data;
}
// creates xml and returns the content
private String getXML2() {
String data="";
TransformerFactory transformerFactory;
Transformer transformer;
DOMSource source;
StreamResult result;
StringWriter writer;
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root elements
Document doc = docBuilder.newDocument();
Element soapEnvelope = doc.createElement("soapenv:Envelope");
soapEnvelope.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
soapEnvelope.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
soapEnvelope.setAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
soapEnvelope.setAttribute("xmlns:ns0", "https://ws.mycompany.com/");
doc.appendChild(soapEnvelope);
//elements
Element header = doc.createElement("soapenv:Header");
soapEnvelope.appendChild(header);
Element soapBody = doc.createElement("soapenv:Body");
soapEnvelope.appendChild(soapBody);
Element ns0 = doc.createElement("ns0:SampleApplication");
ns0.setAttribute("xmlns:ns0", "http://ws.mycompany.com");
soapBody.appendChild(ns0);
Element element01 = doc.createElement("Sample1");
element01.appendChild(doc.createTextNode("value1"));
ns0.appendChild(element01);
Element element02 = doc.createElement("Sample2");
element02.appendChild(doc.createTextNode("value2"));
ns0.appendChild(element02);
Element element03 = doc.createElement("Sample3");
element03.appendChild(doc.createTextNode("value3"));
ns0.appendChild(element03);
writer = new StringWriter();
result = new StreamResult(writer);
transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
source = new DOMSource(doc);
transformer.transform(source, result);
data = writer.getBuffer().toString();
} catch(ParserConfigurationException pce){
pce.printStackTrace();
} catch(TransformerException tfe){
tfe.printStackTrace();
}
return data;
}
public static void main(String args[]) {
new CreateXMLClass();
System.exit(0);
}
}