У меня есть XML-файл, который выглядит следующим образом
<?xml version="1.0" encoding="UTF-8" ?>
<BulkDataExchangeRequests xmlns="urn:ebay:apis:eBLBaseComponents">
<Header>
<Version>583</Version>
<SiteID>0</SiteID>
</Header>
<AddFixedPriceItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<ErrorLanguage>en_US</ErrorLanguage>
<WarningLevel>High</WarningLevel>
<Version>583</Version>
<Item>
<CategoryMappingAllowed>true</CategoryMappingAllowed>
<ConditionID>1000</ConditionID>
<Country>US</Country>
<Currency>USD</Currency>
<Description>Minimal fixed-price shoe listing with SKU, free shipping, 3-day dispatch time, return policy, and no Item Specifics. New Nike Shox Elite TB White/White-Black-Chrome. Size: Mens US 12, UK 11, Europe 46 (Medium, D, M). Condition: New in box.</Description>
<DispatchTimeMax>3</DispatchTimeMax>
<InventoryTrackingMethod>SKU</InventoryTrackingMethod>
<ListingDuration>Days_30</ListingDuration>
<ListingType>FixedPriceItem</ListingType>
<Location>San Jose, CA</Location>
<PaymentMethods>PayPal</PaymentMethods>
<PayPalEmailAddress>MegaOnlineMerchant@gmail.com</PayPalEmailAddress>
<PrimaryCategory>
<CategoryID>63850</CategoryID>
</PrimaryCategory>
<Quantity>6</Quantity>
<ReturnPolicy>
<ReturnsAcceptedOption>ReturnsAccepted</ReturnsAcceptedOption>
<RefundOption>MoneyBack</RefundOption>
<ReturnsWithinOption>Days_30</ReturnsWithinOption>
<Description>Text description of return policy details here.</Description>
<ShippingCostPaidByOption>Buyer</ShippingCostPaidByOption>
</ReturnPolicy>
<ShippingDetails>
<ShippingType>Flat</ShippingType>
<ShippingServiceOptions>
<ShippingServicePriority>1</ShippingServicePriority>
<ShippingService>USPSPriority</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost>0.00</ShippingServiceAdditionalCost>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
</ShippingDetails>
<Site>US</Site>
<SKU>1122334455-36</SKU>
<StartPrice>50.00</StartPrice>
<Title>Latest Nike Shox Elite TB White Mens Basketball Shoes S 12</Title>
<UUID>7d004a30b0f511ddad8b0807654c9a55</UUID>
</Item>
</AddFixedPriceItemRequest>
, когда я изменяю этот XML-код из Java, чтобы добавить новый UUID, элемент AddFixedPriceItemRequest теряет свой xmlns = "urn: ebay: apis: eBLBaseComponents "атрибут.Я использую следующий код.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(path + System.getProperty("file.separator") + "sc_input" + System.getProperty("file.separator") + "input.xml"));
NodeList list = doc.getElementsByTagName("UUID");
for (int i = 0; i < list.getLength(); i++) {
// Get element
Element element = (Element) list.item(i);
System.out.println(element.getTextContent());
element.setTextContent(java.util.UUID.randomUUID().toString().replace("-", ""));
}
//setting up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
//generating string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
//Saving the XML content to File
OutputStream f0;
byte buf[] = xmlString.getBytes();
f0 = new FileOutputStream(path + System.getProperty("file.separator") + "sc_input" + System.getProperty("file.separator") + "input.xml");
for (int i = 0; i < buf.length; i++) {
f0.write(buf[i]);
}
f0.close();
buf = null;
Я попытался исправить это, установив пространство имен для этого конкретного элемента с помощью следующего
NodeList nodeList = doc.getElementsByTagName("AddFixedPriceItemRequest");
for(int j = 0; j < nodeList.getLength(); j++){
Element element = (Element) nodeList.item(j);
if(!element.hasAttributes()){
element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns","urn:ebay:apis:eBLBaseComponents");
}
System.out.println(element.getNodeValue());
}
, но, похоже, это не работает.