1 37 38 package com.sun.j2ee.blueprints.opc.invoice; 39 40 import java.util.*; 41 import java.text.*; 42 import java.io.*; 43 import javax.xml.parsers.*; 44 import org.w3c.dom.*; 45 import org.xml.sax.*; 46 import javax.xml.transform.*; 47 import javax.xml.transform.dom.*; 48 import javax.xml.transform.stream.*; 49 50 public class Invoice implements Serializable { 51 52 protected String invoiceId; 53 protected String opcPoId; 54 protected String supplierId; 55 protected String status; 56 57 public Invoice() {} 59 60 public Invoice(String invoiceId, String opcPoId, String supplier, 61 String status) { 62 this.invoiceId = invoiceId; 63 this.opcPoId = opcPoId; 64 this.supplierId = supplier; 65 this.status = status; 66 } 67 68 public String getInvoiceId() { 70 return invoiceId; 71 } 72 73 public String getOpcPoId() { 74 return opcPoId; 75 } 76 77 public String getSupplierId() { 78 return supplierId; 79 } 80 81 public String getStatus() { 82 return status; 83 } 84 85 public void setInvoiceId(String invoiceId) { 87 this.invoiceId = invoiceId; 88 } 89 90 public void setOpcPoId(String id) { 91 this.opcPoId = id; 92 } 93 94 public void setSupplierId(String id) { 95 this.supplierId = id; 96 } 97 98 public void setStatus(String stat) { 99 this.status = stat; 100 } 101 102 104 public static Invoice fromXML(String invDoc) throws XMLException { 105 Invoice invObj = null; 106 try { 107 108 InputSource source = new InputSource(new StringReader(invDoc)); 109 DocumentBuilderFactory docBuilderFactory = 110 DocumentBuilderFactory.newInstance(); 111 docBuilderFactory.setNamespaceAware(true); 112 DocumentBuilder docBuilder = 113 docBuilderFactory.newDocumentBuilder(); 114 115 Document doc = docBuilder.parse(source); 117 Element elem = (Element)doc.getDocumentElement().getFirstChild(); 118 invObj = new Invoice(); 119 invObj.setInvoiceId(((Text)(elem.getFirstChild())).getData()); 120 elem = getNextSibling(elem); 121 invObj.setOpcPoId(((Text)(elem.getFirstChild())).getData()); 122 elem = getNextSibling(elem); 123 invObj.setSupplierId(((Text)(elem.getFirstChild())).getData()); 124 elem = getNextSibling(elem); 125 invObj.setStatus(((Text)(elem.getFirstChild())).getData()); 126 } catch(Exception exe){ 127 throw new XMLException("Invoice XML Exception"); 128 } 129 return invObj; 130 } 131 132 public static Element getNextSibling(Element elem) { 133 for(Node sib=elem.getNextSibling(); sib!=null; 134 sib=sib.getNextSibling()){ 135 if(sib.getNodeType() == Node.ELEMENT_NODE){ 136 return (Element) sib; 137 } 138 } 139 return null; 140 } 141 } 142 | Popular Tags |