【问题标题】:XMLNS attribute getting stripped while writing the DOM on the file在文件上写入 DOM 时,XMLNS 属性被剥离
【发布时间】:2011-11-28 14:14:06
【问题描述】:

我有一个看起来像这样的 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>

当我从 java 修改此 xml 以放置新的 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());
   }

但这似乎不起作用。

【问题讨论】:

    标签: java xml dom xml-namespaces


    【解决方案1】:

    是的,这是完全有效的输出。由于 AddFixedPriceItemRequest 元素嵌套在 BulkDataExchangeRequests 元素下,因此 AddFixedPriceItemRequest 元素上的 xmlns 声明是多余的。

    更新: 如果出于某种原因您需要冗余 xmlns,您可以尝试为 BulkDataExchangeRequests 元素使用不同的命名空间前缀。

    【讨论】:

    • 我知道,一旦在根目录下声明了它,就不需要为 AddFixedPriceItemrequest 声明它,但 Ebay 需要这种方式。我需要一些方法将其放回原处,以便 Ebay 正确处理我的文件。
    • @Amit - 真的吗?令我惊讶的是,如此大的公司需要非标准的 xml 语义。如果它不存在会怎样?
    • 我收到以下错误 失败SystemError 如果我没有为 AddFixedPricedItem 放置 xmlns
    • 但如果我将命名空间添加到 AddFixedPriceItem 标签,Ebay 会毫无问题地处理它。
    • @Amit - 你尝试过我对不同前缀的建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2020-09-04
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多